在目标c中,它可以在init方法中完成
-(id)init{
self = [[[NSBundle mainBundle] loadNibNamed:@"ViewBtnWishList" owner:0 options:nil] objectAtIndex:0];
return self;
}
Run Code Online (Sandbox Code Playgroud)
但是当我快速地这样做时
init(frame: CGRect) {
self = NSBundle.mainBundle().loadNibNamed("ViewDetailMenu", owner: 0, options: nil)[0] as? UIView
}
Run Code Online (Sandbox Code Playgroud)
无法在方法中分配给self自己显示错误.现在我的方法是创建一个视图,并将从nib加载的视图添加到它.谁都有更好的主意?
dre*_*wag 126
对于Swift 4
extension UIView {
class func loadFromNibNamed(nibNamed: String, bundle: Bundle? = nil) -> UIView? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiate(withOwner: nil, options: nil)[0] as? UIView
}
}
Run Code Online (Sandbox Code Playgroud)
对于Swift 3
你可以在UIView上创建一个扩展:
extension UIView {
class func loadFromNibNamed(nibNamed: String, bundle: NSBundle? = nil) -> UIView? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(nil, options: nil)[0] as? UIView
}
}
Run Code Online (Sandbox Code Playgroud)
注意:使用UINib更快,因为它会为您进行缓存.
然后你可以这样做:
ViewDetailItem.loadFromNibNamed("ViewBtnWishList")
Run Code Online (Sandbox Code Playgroud)
并且您将能够在任何视图上重用该方法.
kar*_*agu 24
在Xcode 7 beta 4,Swift 2.0中测试过.以下代码将xib分配给UIView
.您可以在故事板中使用此自定义xib视图并访问该IBOutlet
对象.
import UIKit
@IBDesignable class SimpleCustomView:UIView
{
var view:UIView!;
@IBOutlet weak var lblTitle: UILabel!
@IBInspectable var lblTitleText : String?
{
get{
return lblTitle.text;
}
set(lblTitleText)
{
lblTitle.text = lblTitleText!;
}
}
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib ()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib ()
}
func loadViewFromNib() {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "SimpleCustomView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(view);
}
}
Run Code Online (Sandbox Code Playgroud)
以编程方式访问customview
self.customView = SimpleCustomView(frame: CGRectMake(100, 100, 200, 200))
self.view.addSubview(self.customView!);
Run Code Online (Sandbox Code Playgroud)
源代码 - https://github.com/karthikprabhuA/CustomXIBSwift
Dav*_*ave 23
这对我有用.
override func awakeAfterUsingCoder(aDecoder: NSCoder) -> AnyObject? {
if self.subviews.count == 0 {
return loadNib()
}
return self
}
private func loadNib() -> YourCustomView {
return NSBundle.mainBundle().loadNibNamed("YourCustomViewNibName", owner: nil, options: nil)[0] as YourCustomView
}
Run Code Online (Sandbox Code Playgroud)
hol*_*lex 17
这可能是你的解决方案:
class func instanceFromNib() -> UIView {
return UINib(nibName: "<<NibFileName>>", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
Run Code Online (Sandbox Code Playgroud)
class func instanceFromNib() -> UIView {
return UINib(nibName: "<<NibFileName>>", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as UIView
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
64506 次 |
最近记录: |