从没有故事板的 xib 加载 UIView
Load UIView from xib without storyboard
我正在尝试以编程方式加载 nib,而不是在情节提要中使用 IBOutlets。
我已经设置了文件所有者的自定义 class 并为图像和标签创建了出口。我已经创建了一个 UINib 实例,并在我的自定义 class 中调用了 instantiateWithOwner 来手动加载笔尖。现在我正在尝试在 uitableviewcontroller 上加载笔尖。
我的问题是如何在不使用情节提要将 uiview 添加到视图控制器且不将 outlet 连接到 uiview 的情况下将 xib 加载到视图控制器上。
关于如何使用故事板和 IBOutlets 加载 nib 有几个问题和教程,很简单。
谁能告诉我如何在 swift 中以编程方式解决这个问题?
注意:我的项目中有两个视图控制器。 VC1 有一个带有图像和标签的 collectionView。我正在使用 xib 和自定义 class 在 VC2 中显示选定的 collectionView。
您不一定要有所有者设置。您可以简单地实例化 nib 并引用它创建的顶级对象。话虽如此,如果您确实将 self 作为所有者并连接了网点,那也可以。值得注意的是,如果多次实例化 nib,每次都会将 outlet 的值设置为新实例。如果你想引用之前的那些,你必须在其他 属性.
中捕获它们
// Create a UINib object for a nib named MyNib in the main bundle
let nib = UINib(nibName: "MyNib", bundle: nil)
// Instante the objects in the UINib
let topLevelObjects = nib.instantiateWithOwner(self /*or nil*/, options: nil)
// Use the top level objects directly...
let firstTopLevelObject = topLevelObjects[0]
// Or use any IBOutlets
doSomeStuff(self.someOutletProperty)
您可以从 XIB 获取俯视图并将其放在任何地方
override func viewDidLoad() {
super.viewDidLoad()
let calendar = UINib(nibName: "<<NibFileName>>", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
view.addSubview(calendar)
}
你可以使用这个方法:
方法:
public static func getViewFromNib(nibName:String)-> UINib{
let uiNib = UINib(nibName: nibName, bundle: nil)
return uiNib
}
用法:
let customView = getViewFromNib.instantiateWithOwner(ownerOrNil, options: nil) as! UIView
view.addSubview(customView)
感谢您回答这个问题。他们都很好。我接受了对我来说最容易实施的那个。这里有一种不同的方法也适用于某些情况:
var myView: myCoolView = myCoolView() // subclass of uiview with xib
然后,在ViewDidLoad中添加:
myView.frame = CGRectMake(0, 64, self.view.frame.size.width, 175)
myView.titleLabel.text = stringOne
myView.backgroundImage.image = imageOne
myView.contentMode = UIViewContentMode.ScaleAspectFill
myView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
self.view.addSubview(myView)
我从 here 找到了另一个解决方案并为我改编 (Swift 2):
let myView = NSBundle.mainBundle().loadNibNamed("myView", owner: self, options: nil).first as! myView
self.view.addSubview(myView)
myView.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":myView]))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":myView]))
myView.myButton.setTitle("myTitle", forState: .Normal)
以编程方式创建的约束。在 xib 中创建的 outlets 在主要 viewController class.
中很容易获得
无论如何,这段代码肯定会得到改进。我是一个粗糙的程序员。
最佳答案不适合我。我刚刚完成:
let newView = Bundle.main.loadNibNamed("MyNib", owner: self, options: nil)?.first as! UIView
self.view.addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":newView]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":newView]))
很有魅力
如果您需要访问某个元素,您可以这样做:
newView.myButton.setTitle("myTitle", forState: .Normal)
写得不错!
我正在尝试以编程方式加载 nib,而不是在情节提要中使用 IBOutlets。
我已经设置了文件所有者的自定义 class 并为图像和标签创建了出口。我已经创建了一个 UINib 实例,并在我的自定义 class 中调用了 instantiateWithOwner 来手动加载笔尖。现在我正在尝试在 uitableviewcontroller 上加载笔尖。
我的问题是如何在不使用情节提要将 uiview 添加到视图控制器且不将 outlet 连接到 uiview 的情况下将 xib 加载到视图控制器上。
关于如何使用故事板和 IBOutlets 加载 nib 有几个问题和教程,很简单。
谁能告诉我如何在 swift 中以编程方式解决这个问题?
注意:我的项目中有两个视图控制器。 VC1 有一个带有图像和标签的 collectionView。我正在使用 xib 和自定义 class 在 VC2 中显示选定的 collectionView。
您不一定要有所有者设置。您可以简单地实例化 nib 并引用它创建的顶级对象。话虽如此,如果您确实将 self 作为所有者并连接了网点,那也可以。值得注意的是,如果多次实例化 nib,每次都会将 outlet 的值设置为新实例。如果你想引用之前的那些,你必须在其他 属性.
中捕获它们 // Create a UINib object for a nib named MyNib in the main bundle
let nib = UINib(nibName: "MyNib", bundle: nil)
// Instante the objects in the UINib
let topLevelObjects = nib.instantiateWithOwner(self /*or nil*/, options: nil)
// Use the top level objects directly...
let firstTopLevelObject = topLevelObjects[0]
// Or use any IBOutlets
doSomeStuff(self.someOutletProperty)
您可以从 XIB 获取俯视图并将其放在任何地方
override func viewDidLoad() {
super.viewDidLoad()
let calendar = UINib(nibName: "<<NibFileName>>", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
view.addSubview(calendar)
}
你可以使用这个方法:
方法:
public static func getViewFromNib(nibName:String)-> UINib{
let uiNib = UINib(nibName: nibName, bundle: nil)
return uiNib
}
用法:
let customView = getViewFromNib.instantiateWithOwner(ownerOrNil, options: nil) as! UIView
view.addSubview(customView)
感谢您回答这个问题。他们都很好。我接受了对我来说最容易实施的那个。这里有一种不同的方法也适用于某些情况:
var myView: myCoolView = myCoolView() // subclass of uiview with xib
然后,在ViewDidLoad中添加:
myView.frame = CGRectMake(0, 64, self.view.frame.size.width, 175)
myView.titleLabel.text = stringOne
myView.backgroundImage.image = imageOne
myView.contentMode = UIViewContentMode.ScaleAspectFill
myView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
self.view.addSubview(myView)
我从 here 找到了另一个解决方案并为我改编 (Swift 2):
let myView = NSBundle.mainBundle().loadNibNamed("myView", owner: self, options: nil).first as! myView
self.view.addSubview(myView)
myView.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":myView]))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":myView]))
myView.myButton.setTitle("myTitle", forState: .Normal)
以编程方式创建的约束。在 xib 中创建的 outlets 在主要 viewController class.
中很容易获得无论如何,这段代码肯定会得到改进。我是一个粗糙的程序员。
最佳答案不适合我。我刚刚完成:
let newView = Bundle.main.loadNibNamed("MyNib", owner: self, options: nil)?.first as! UIView
self.view.addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":newView]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":newView]))
很有魅力
如果您需要访问某个元素,您可以这样做:
newView.myButton.setTitle("myTitle", forState: .Normal)
写得不错!