22.Storyboard中使用xib,xib中使用xib并实现预览

storyboard中能够插入xib定义的view,并能够预览,xib中插入xib使用方法和storyboard是一样的方法.(Swift3.0版)

  1. 先新建好自定义的xib和对应的类,注意类名必须和xib中绑定的类名一致,否则无法在类中关联xib中的组件(进行连线)


    Snip20160926_4.png

自定义类的代码如下:

import UIKit

@IBDesignable
class CustomView: UIView {
    
    @IBOutlet weak var label: UILabel!
    @IBAction func onTouchUp(_ sender: UIButton) {
        onButtonRun?()
    }
    
    @IBInspectable
    var name:String?{
        get{
            return label.text
        }
        set{
            label.text = newValue
        }
    }
    
    var onButtonRun:(()->Void)?
    //初始化时将xib中的view添加进来
    var contentView:UIView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        addSubview(contentView)
        addConstraints()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        contentView = loadViewFromNib()
        addSubview(contentView)
        addConstraints()
    }
    //加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        return view
    }
    //设置好约束
    func addConstraints() {
        contentView.translatesAutoresizingMaskIntoConstraints = false
        
        var constraint = NSLayoutConstraint(item: contentView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
        addConstraint(constraint)
    }
}

2.在storyboard中拖入一个View组件,然后将class设置为我们自定义的类名"CustomView"


Paste_Image.png

3.这是我在自定义类中增加的可以编辑的属性,修改也能够实时预览它


Snip20160926_6.png

源码实现:https://github.com/ghyh22/StoryboardInsertXibView

参考:http://swiftdeveloperblog.com/creating-custom-user-interface-files-with-xib-in-xcode-6-and-swift/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,469评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,207评论 4 61
  • 昨天和男票吵架,吵到撕心裂肺,声泪俱下,他说和我在一起是为了还债,当我听到这句话的时候,就听见了自己心碎的声音,我...
    景sunny阅读 7,015评论 0 0
  • 胡言乱语了一些东西! 原来总觉得人与人之间应该开诚布公地,若能做到知根知底那就最好了。而且我更是一直相信随着微信的...
    拥有一颗玻璃心的帅帅阅读 1,859评论 0 0
  • 恋爱真是个美好的事情,两个人可以一起旅行,一起欣赏沿途风景,手牵手去散步,一起学习,一起吃饭等等,在学校里看...
    木子木昜阅读 3,945评论 0 0