UIToolbar xib xcode [英] UIToolbar xib xcode
本文介绍了UIToolbar xib xcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个可以添加到多个视图的自定义UIToolbar,我不想以编程方式执行此操作,因此我使用xib来设计UIToolbar。我需要将它包装在UIView和View Controller中吗? ViewController.m是根视图控制器。这只是一个简单的项目我只是尝试了一些东西
I am trying to create a custom UIToolbar that I can add to multiple views, I don't want to do this programmatically so I am using xib to design a UIToolbar. Do I need to wrap this in a UIView and a View Controller? ViewController.m is the root view controller. This is just a simple project I am just trying out some stuff
推荐答案
你可以这样做:
- 创建ToolBar.xib文件,删除UIView并添加UIToolbar
- 创建ToolBar.swift文件,添加代码,如图所示,并设置出口
- for .xib inIdentity and Typeput nameToolBar.swift
-
在你的根视图控制器中将此代码放在ViewDidLoad中:
In your root View Controller put this code in ViewDidLoad:
let toolBar = UINib(nibName: "ToolBar", bundle: nil).instantiateWithOwner(nil, options: nil).first as! ToolBar
toolBar.hidden = true
self.navigationController!.view.addSubview(toolBar)
self.navigationController!.toolbarItems = toolBar.items
放入ViewDidLoad的所有View控制器:
In all View Controllers put in ViewDidLoad:
self.toolbarItems = self.navigationController!.toolbarItems
ToolBar.swift:
ToolBar.swift:
import UIKit
class ToolBar: UIToolbar {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBAction func item1Pressed(sender: AnyObject) {
print("item 1 pressed")
}
@IBAction func item2Pressed(sender: AnyObject) {
print("item 2 pressed")
}
@IBAction func item3Pressed(sender: AnyObject) {
print("item 3 pressed")
}
@IBAction func item4Pressed(sender: AnyObject) {
print("item 4 pressed")
}
}
这篇关于UIToolbar xib xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文