首页 文章

从没有故事板的xib加载UIView

提问于
浏览
0

我试图以编程方式加载一个笔尖而不在故事板中使用IBOutlets .

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

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

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

谁能告诉我如何以编程方式快速进行此操作?

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

6 回答

  • 0

    你可以从顶视图形式XIB并把它放在任何地方

    override func viewDidLoad() {
        super.viewDidLoad()
        let calendar = UINib(nibName: "<<NibFileName>>", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
        view.addSubview(calendar)
    }
    
  • 1

    您可以使用此方法:

    Method:

    public static func getViewFromNib(nibName:String)-> UINib{
        let uiNib = UINib(nibName: nibName, bundle: nil)
        return uiNib 
    }
    

    Usage :

    let customView = getViewFromNib.instantiateWithOwner(ownerOrNil, options: nil) as! UIView
        view.addSubview(customView)
    
  • 1

    您不一定要拥有所有者 . 您可以简单地实例化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)
    
  • 0

    我找到了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类中轻松获得 .

    无论如何,这个代码肯定会得到改进 . 我是一个粗略的程序员 .

  • 1

    谢谢你回答这个问题 . 他们都很好 . 我接受了最容易实现的那个 . 这是一种不同的方法,也适用于某些情况:

    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)
    
  • 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]))
    

    works like a charm

    如果您需要访问元素,您可以执行以下操作:

    newView.myButton.setTitle("myTitle", forState: .Normal)
    

    好编码!

相关问题