如何在导航栏ios的中心获得两个按钮

时间:2016-01-04 04:20:46

标签: ios swift uinavigationcontroller uinavigationbar

enter image description here

我想在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的变量。

生成的导航标题如下所示,无法弄清楚我出错的地方。视图堆叠在一起。

enter image description here

1 个答案:

答案 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