从没有故事板的xib加载UIView

时间:2015-10-02 20:27:21

标签: swift uiview xib

我正在尝试以编程方式加载nib,而不在故事板中使用IBOutlets。

我已经设置了文件所有者的自定义类,并为图像和标签创建了出口。我已经创建了一个UINib实例,并在我的自定义类中调用了instantiateWithOwner来手动加载nib。现在我正在尝试在uitableviewcontroller上加载nib。

我的问题是如何在视图控制器上加载xib而不使用故事板向视图控制器添加uiview并且不连接uiview的插座。

关于如何使用storyboard和IBOutlets加载笔尖有几个问题和教程,这很简单。

有人能告诉我如何以编程方式进行此操作吗?

注意:我的项目中有两个viewcontrollers。 VC1有一个带有图像和标签的collectionView。我正在使用xib和自定义类在VC2中显示所选的collectionView。

6 个答案:

答案 0 :(得分:1)

您可以从顶部视图表格XIB并将其放在任何地方

sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl

答案 1 :(得分:1)

您可以使用此方法:

方式:

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)

答案 2 :(得分:1)

我找到了来自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中创建的出口可以在主viewController类中轻松获得。

无论如何,这段代码肯定会有所改进。我是一个粗略的程序员。

答案 3 :(得分:0)

您不一定要拥有所有者。您可以简单地实例化nib并引用它创建的顶级对象。话虽如此,如果你确实通过自己作为所有者和连接出口,这也是有效的。值得注意的是,如果多次实例化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)

答案 4 :(得分:0)

感谢您回答这个问题。他们都很好。我已经接受了对我来说最容易实施的那个。这是一种不同的方法,也适用于某些情况:

    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)

答案 5 :(得分:0)

最好的答案对我没用。我刚刚完成了:

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)

良好的编码!