如何在XCode 6中水平并排2个标签?

时间:2015-04-24 11:39:14

标签: ios xcode autolayout

这是我需要的结果:

enter image description here

标签宽度不固定。它们可以相等,首先可以大于第二个,第二个可以大于第一个。我在其他问题中找到了一些解决方案,但它并不能让我满意,因为我不想添加额外的视图而且我的宽度不固定。

2 个答案:

答案 0 :(得分:8)

我做了一些研究,没有额外的观点仍然是不可能的: - )

但最简单的方法是:

<强> 1。包装器的约束:

enter image description here

<强> 2。嵌入式视图的约束:

enter image description here enter image description here

第3。结果如下:

enter image description here

enter image description here

答案 1 :(得分:1)

此处介绍如何以编程方式进行

let margins = view.safeAreaLayoutGuide
//Let x equal whatever you want your height to be 
//Let y equal whatever you want your width to be 

view1.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
view1.trailingAnchor.constraint(equalTo: margins.centerXAnchor).isActive = true
view1.widthAnchor.constraint(equalToConstant: X).isActive = true
view1.heightAnchor.constraint(equalToConstant: Y).isActive = true

view2.leadingAnchor.constraint(equalTo: margins.centerXAnchor, constant: 8).isActive = true
view2.topAnchor.constraint(equalTo: view2.topAnchor).isActive = true
view2.bottomAnchor.constraint(equalTo: view2.bottomAnchor).isActive = true
view2.widthAnchor.constraint(equalToConstant: X).isActive = true