2015-08-14 70 views
0

我想要一个标签在工具栏的UIBarButtonItem上方浮动。我已经能够限制它,以便它正好在工具栏上方,但我无法弄清楚如何使其与012B匹配UIBarButtonItem的center.x。我如何使用AutoLayout来做到这一点?限制视图到UIBarButtonItem的位置

回答

1

我结束了在下列方式解决这个:

(由于的UIBarButtonItem是不是一个视图,你不能直接添加约束它,因此,我们使用valueForKey:检索视图)

if let itemView = myBarButtonItem.valueForKey("view") as? UIView { 
     view.addConstraint(NSLayoutConstraint(item: myLabel, 
      attribute: .CenterX, 
      relatedBy: .Equal, 
      toItem: itemView, 
      attribute: .CenterX, 
      multiplier: 1, 
      constant: 0)) 
} 

注:我只能用我的工具栏手动添加到视图,它的无线测试这将无法使用导航控制器的工具栏

1

不能这样做与UIBarButtonItem

随着UIButton的,你可以单击标签或按钮,然后选择控制拖动到其他,然后选择“中心X”选项

+1

UIBarButtonItem不是一个视图,所以不幸的是,这是不可能的... –