美文网首页Swift开发学习
Swift开发小技巧日志(一)

Swift开发小技巧日志(一)

作者: 追风观雪 | 来源:发表于2017-06-10 18:26 被阅读0次

    个人学习记录,未必严谨,不喜勿喷!


    1、键盘加入自定义按钮

    实现思路:定义一个自定义返回UIToolbar类型的函数,UIToolbar上面有自定义的按钮,然后让调用键盘的比如UITextView的inputAccessoryView属性等于该函数。

    关键代码:

    messageTextView?.inputAccessoryView = addToolbar()

    -----------------------------------------------------------

    func addToolbar() -> UIToolbar {

    var toolbar: UIToolbar = UIToolbar();

    toolbar.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35)

    toolbar.tintColor = UIColor.blue;

    toolbar.backgroundColor = UIColor.gray;

    var pre: UIBarButtonItem = UIBarButtonItem(title: "上一步", style: UIBarButtonItemStyle.plain, target: self, action: #selector(textFieldpre))

    var next: UIBarButtonItem = UIBarButtonItem(title: "下一步", style: UIBarButtonItemStyle.plain, target: self, action: #selector(textFieldnext))

    var space: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)

    var bar: UIBarButtonItem = UIBarButtonItem(title: "完成", style: UIBarButtonItemStyle.plain, target: self, action: #selector(textFieldnext))

    toolbar.items = [pre, next, space, bar]

    return toolbar;

    }


    2、UILable支持多行

    实现思路:原以为设置了Lable.numberOfLines = 0即可,实际应用时还需要同时设置Lable.lineBreakMode = NSLineBreakMode.byWordWrapping


    3、UILable、UIButton等设置成圆角或圆形

    实现思路:除了要设置 Lable.layer.cornerRadius = 10,还要将此设置应用到Layer层,即同时需要设置:Lable.layer.masksToBounds = true


    4、自定义UITableViewCell实现步骤

    实现思路:

    (1)、首先创建单独Cell类文件,继承自UITableViewCell;

    (2)、重载override init(style: UITableViewCellStyle, reuseIdentifier: String?) 方法;

    (3)、在Init方法里完成界面布局;

    (4)、在有UITableView的Controler中继承数据源协议UITableViewDataSource;

    (5)、实现func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int  和  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 方法,在第2个方法里返回自定义Cell;


    5、使用SnapKit实现UITableViewCell高度自适应

    实现思路:

    (1)、Pod安装SnapKit;

    (2)、设置Cell里的控件布局,同时设置容器contentView的布局;

    (3)、设置UITableView的属性;

    关键代码:

    UITableViewCell 里的代码  --------------------

    msgShortContent.snp.makeConstraints { (make) in

    make.leading.equalTo(self).offset(55)

    make.trailing.equalToSuperview().offset(-5)

    make.top.equalToSuperview().offset(35)

    make.left.equalToSuperview().offset(55)

    }

    contentView.snp.makeConstraints { (make) in

    make.bottom.equalTo(msgShortContent.snp.bottom).offset(10)

    make.leading.equalTo(self)

    make.top.equalTo(self)

    make.trailing.equalTo(self)

    }

    UITableView 里的代码,有2个属性必须设置  --------------------

    msgListTableView?.estimatedRowHeight = 300

    msgListTableView?.rowHeight = UITableViewAutomaticDimension


    6、TextView的text值被减少字符量重置后会在尾部留下空白问题解决

    解决思路:因为UITextView继承于UIScrollView,要将其滚动的属性关掉。

    messageTextView?.isScrollEnabled = false


    7、在UITableViewCell被选中时UILable的标签的背景色消失问题解决

    解决思路:重写 setSelected 、setHighlighted方法,在选中前选保存背景色,在选中后再重新赋值给标签。

    关键代码:

    /// 重写 setSelected 、setHighlighted解决cell选中时标签背景色丢失的问题

    override func setSelected(_ selected: Bool, animated: Bool) {

    let originColor = msgCountByType.backgroundColor

    super.setSelected(selected, animated: animated)

    msgCountByType.backgroundColor = originColor

    }

    override func setHighlighted(_ highlighted: Bool, animated: Bool) {

    let originColor = msgCountByType.backgroundColor

    super.setHighlighted(highlighted, animated: animated)

    msgCountByType.backgroundColor = originColor

    }


    相关文章

      网友评论

        本文标题:Swift开发小技巧日志(一)

        本文链接:https://www.haomeiwen.com/subject/fjqcqxtx.html