重用故事板一个UIView厦门国际银行 [英] Reuse a uiview xib in storyboard
本文介绍了重用故事板一个UIView厦门国际银行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通常喜欢创建和设计我uiviews接口生成器。有时候,我需要建立在厦门国际银行的单一视图,可以在多个视图控制器在故事板可重复使用。
解决方案
再利用和渲染Storyboard的厦门国际银行。
的与雨燕2.2与放大器测试; X $ C $ç7.3.1 的
1 ----创建一个名为DesignableXibView
新的UIView- 文件>新建>文件>源>可可触摸类>的UIView
2 ----创建一个名为匹配的厦门国际银行文件'DesignableXibView H2>
- 文件>新建>文件>用户界面>视图
3 ----设置的厦门国际银行的
的文件所有者
- 选择厦门国际银行
- 选择文件的所有者
- 设置自定义类在Identity督察DesignableXibView。
- 的注意:不要设置自定义类的厦门国际银行的观点。只有文件所有者!的
4 ---- DesignableXibView的实施
// DesignableXibView.swift进口的UIKit@IBDesignable类DesignableXibView:UIView的{ VAR内容查看:UIView的? 覆盖的init(帧:的CGRect){
super.init(帧:帧)
xibSetup()
} 所需的init(codeR ADE codeR:NS codeR){
super.init(codeR:ADE codeR)
xibSetup()
} FUNC xibSetup(){
内容查看= loadViewFromNib() //使用范围不是帧或者它会被抵消
内容查看!.frame =界限 //使视图舒展含看法
内容查看!.autoresizingMask = [UIViewAutoresizing.FlexibleWidth,UIViewAutoresizing.FlexibleHeight] //我们的观点之上添加自定义子视图(在任何自定义绘制>请参阅下面的注释)
addSubview(内容查看!)
} FUNC loadViewFromNib() - > UIView的! { 让包=一个NSBundle(forClass:self.dynamicType)
让笔尖= UINib(nibName:字符串(self.dynamicType),包:包)
让视图= nib.instantiateWithOwner(个体经营,选项:无)[0]作为!的UIView 返回查看
}}
5 ----测试您的可重复使用的视图故事板
- 打开你的故事板
- 添加视图
- 设置该视图的自定义类
- 的等一下... 的BOOM!
I typically like to create and design my uiviews in interface builder. Sometimes I need to create a single view in a xib that can be reused in multiple view controllers in a storyboard.
解决方案
Reuse and render a xib in a storyboard.
Tested with Swift 2.2 & Xcode 7.3.1
1 ---- Create a new UIView named 'DesignableXibView'
- File > New > File > Source > Cocoa Touch Class > UIView
2 ---- Create a matching xib file named 'DesignableXibView'
- File > New > File > User Interface > View
3 ---- Set the file owner of the of the xib
- select the xib
- select file's owner
- set custom class to 'DesignableXibView' in the Identity Inspector.
- Note: Do not set the custom class of the view on the xib. Only the File Owner!
4 ---- DesignableXibView's Implementation
// DesignableXibView.swift
import UIKit
@IBDesignable
class DesignableXibView: UIView {
var contentView : UIView?
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
func xibSetup() {
contentView = loadViewFromNib()
// use bounds not frame or it'll be offset
contentView!.frame = bounds
// Make the view stretch with containing view
contentView!.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
// Adding custom subview on top of our view (over any custom drawing > see note below)
addSubview(contentView!)
}
func loadViewFromNib() -> UIView! {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: String(self.dynamicType), bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
}
5 ---- Test your reuseable view in a storyboard
- Open your storyboard
- Add a view
- Set that view's Custom Class
- wait a sec ... BOOM!!
这篇关于重用故事板一个UIView厦门国际银行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文