2016-12-01 173 views
0

我可以在Xib文件中创建自定义UIToolBar吗?使用xib自定义UIToolBar [Swift 3]

我想制作一个工具栏,在最左侧设置一个完成按钮,在左侧设置最后一个/下一个按钮。但我想确保约束条件能够针对每个手机尺寸正确缩放。有没有例子?

+0

只想添加这个环节,我搞砸了几次,不得不这样做是为了让一切运行正确。 http://stackoverflow.com/questions/29923881/could-not-insert-new-outlet-connection-could-not-find-any-information-for-the-c – rutheferd

回答

2

是的,你可以。首先选择一个xib作为模板。命名模板“工具栏”。 create the xib

然后删除它给你的默认视图。转到右下角的对象库并拖出一个工具栏。 drag out a toolbar xib

您将在最左侧获得一个默认的酒吧按钮项目。将其重命名为“完成”,然后拖出另一个栏按钮项并将其重命名为“Next”或“Last”。 rename bar button items

现在,创建一个新文件。选择Cococa Touch Class并将其设为UIToolBar的一个子类。 enter image description here

返回工具栏xib,并将其类创建为您刚创建的Cocoa Touch类。在我的例子中,我命名了我的ToolBarExample。

现在转到您的ViewController类。在ViewDidLoad中添加以下内容。

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    let toolBar = UINib(nibName: "toolbar", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! ToolBarExample 

    toolBar.frame = CGRect(x: 0, y: self.view.frame.size.height - 46, width: self.view.frame.size.width, height: 46) 

    toolBar.sizeToFit() 

    view.addSubview(toolBar) 

} 

您可以通过调整其框架来更改更改工具栏的比例,大小和位置。您还可以从Atrributes和Size Inspector中调整工具栏xib及其栏按钮项目。该示例的最终产品应该在模拟器中如下所示。

Final Product in Simulator

+0

正是我在找什么!谢谢! – rutheferd