我想在Swift中为我的导航栏实现以下图像。我尝试了以下代码,但是,它只是将按钮叠加在彼此之上
let fromButton:UIButton = UIButton(frame: CGRectMake(0,0,80,44))
let toButton:UIButton = UIButton(frame: CGRectMake(80,0,80,44))
fromButtonView.bounds = fromButton.frame
fromButton.addSubview(fromButtonView)
toButtonView.bounds = toButton.frame
toButton.addSubview(toButtonView)
var myView:UIView = UIView(frame: CGRectMake(0,0,160,44))
myView.addSubview(toButton)
myView.addSubview(fromButton)
self.navigationItem.titleView = myView
fromButtonView和toButtonView是将视图实现为xibs的变量。
生成的导航标题如下所示,无法弄清楚我出错的地方。视图堆叠在一起。
答案 0 :(得分:0)
let testFrame : CGRect = CGRectMake(0,0,180,40)
var buttonView: UIView = UIView(frame: testFrame)
var button1 = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
button.frame = CGRectMake(0, 0, 40, 40) as CGRect
button.backgroundColor = UIColor.redColor()
button.setTitle("Button", forState: UIControlState.Normal)
var button2 = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
button.frame = CGRectMake(70, 0, 40, 40) as CGRect
button.backgroundColor = UIColor.redColor()
button.setTitle("Button", forState: UIControlState.Normal)
buttonView.addSubview(button1)
buttonView.addSubview(button2)
self.navigationItem.titleView = buttonView