2016-06-21 50 views
0

我想实现什么,他们三个人都UILabels:中心UIView没有重叠不同大小的邻居意见?

enter image description here

规格:

  1. leftright修边UIViews。
  2. middle居中,即使侧视图的大小不一样。
  3. middleleftmiddleright之间的碰撞,在middle文本被截断尾巴而leftright不会改变。

问题:

我已经不能满足(2),并在同一时间(3)这两个要求。

  • (2)与middle.autoAlignAxisToSuperviewAxis(.Vertical)
    • 问题来实现的:没有截断,它只是重叠leftLabelrightLabel
  • (3)用middle.autoPinEdge(.Left, toEdge: .Right, ofView: leftLabel)rightLabel的相同策略实现。
    • 问题:middle集中在leftLabelrightLabel之间的区域,如果最后两不具有相同的大小,middle没有的SuperView了居中。

两个应用创建一个约束冲突,我不知道如何解决它,所以我的问题:

我如何中心一个UILabel并截断它avoir与重叠的侧视图。不同的尺寸?

+0

我才拿到你的权利? UIView中嵌入了三个'UILabel'吗? –

+0

如果左侧与中间10px重叠,那么您希望中间仅在左侧还是在左侧和右侧(共20像素)减少10px? – Code

+0

@代码左边只剩下10px。 – oldergod

回答

1

您只需要添加两个约束,一个是中间和左边之间的水平间距,将关系设置为'大于或等于',并且常量为例如10;另一个是中间和右边之间的水平间距,像约束一样的配置。

当中间标签的文本太长时,下面的约束将防止中间标签与标签左右重叠。

添加示例代码:
我想你已经设置的约束UILabel左右在故事板或代码。下面的示例用于configu UILabel中间

func setupConstraint() { 
    self.middle = UILabel() 
    self.middle!.backgroundColor = UIColor.yellowColor() 
    self.view.addSubview(self.middle!) 
    self.middle!.numberOfLines = 0 
    self.middle!.translatesAutoresizingMaskIntoConstraints = false 
    let middle = self.middle! 
    var constraint = NSLayoutConstraint(item: middle, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: left, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0) 

    self.view.addConstraint(constraint) 

    constraint = NSLayoutConstraint(item: middle, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: left, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 10) 

    self.view.addConstraint(constraint) 

    constraint = NSLayoutConstraint(item: right, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: middle, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 10) 
    constraint.priority = 1000 

    self.view.addConstraint(constraint) 

    constraint = NSLayoutConstraint(item: middle, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) 

    self.view.addConstraint(constraint) 

    constraint = NSLayoutConstraint(item: middle, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: middle, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 100) 

    self.view.addConstraint(constraint) 

    self.middle!.text = "self.middle.translatesAutoresizingMaskIntoConstraints = false" 
} 



Sanpshot
enter image description here

+0

你想介绍一下代码中的反映吗?我不知道如何编写这些代码,因为我最近才开始编写代码。 – oldergod

+0

@oldergod你想用代码而不是故事板做到这一点?我会尝试写一些样本。这可能需要一些时间,因为我是一个Objective-C编程器 –

+0

是的,只有代码。 – oldergod

1

我试图设置在游乐场的溶液:

import UIKit 
import XCPlayground 

let viewController = UIViewController() 
XCPlaygroundPage.currentPage.liveView = viewController.view 

viewController.view.backgroundColor = UIColor.whiteColor() 

let leftView = UIView() 
leftView.translatesAutoresizingMaskIntoConstraints = false 
leftView.backgroundColor = UIColor.greenColor() 
let leftLabel = UILabel() 
leftLabel.translatesAutoresizingMaskIntoConstraints = false 
leftLabel.text = "leftleftleftleftleftleftleftleftleftleftleftleftleftleft" 
leftView.addSubview(leftLabel) 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-2-[leftLabel]-2-|", options: [], metrics: nil, views: ["leftLabel": leftLabel])) 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-2-[leftLabel]-2-|", options: [], metrics: nil, views: ["leftLabel": leftLabel])) 

let middleView = UIView() 
middleView.translatesAutoresizingMaskIntoConstraints = false 
middleView.backgroundColor = UIColor.redColor() 
let middleLabel = UILabel() 
middleLabel.translatesAutoresizingMaskIntoConstraints = false 
middleLabel.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, forAxis: .Horizontal) 
middleLabel.text = "middle" 
middleView.addSubview(middleLabel) 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-2-[middleLabel]-2-|", options: [], metrics: nil, views: ["middleLabel": middleLabel])) 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-2-[middleLabel]-2-|", options: [], metrics: nil, views: ["middleLabel": middleLabel])) 

let rightView = UIView() 
rightView.translatesAutoresizingMaskIntoConstraints = false 
rightView.backgroundColor = UIColor.greenColor() 
let rightLabel = UILabel() 
rightLabel.translatesAutoresizingMaskIntoConstraints = false 
rightLabel.text = "right" 
rightView.addSubview(rightLabel) 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-2-[rightLabel]-2-|", options: [], metrics: nil, views: ["rightLabel": rightLabel])) 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-2-[rightLabel]-2-|", options: [], metrics: nil, views: ["rightLabel": rightLabel])) 

viewController.view.addSubview(leftView) 
viewController.view.addSubview(middleView) 
viewController.view.addSubview(rightView) 

NSLayoutConstraint(item: middleView, attribute: .CenterX, relatedBy: .Equal, toItem: viewController.view, attribute: .CenterX, multiplier: 1, constant: 0).active = true 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[leftView]->=8-[middleView]->=8-[rightView]|", options: [.AlignAllTop, .AlignAllBottom], metrics: nil, views: ["leftView": leftView, "middleView": middleView, "rightView": rightView])) 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[leftView]", options: [], metrics: nil, views: ["leftView": leftView])) 

结果像这样:

result

UPDATE(没有嵌入自己的看法标签):

import UIKit 
import XCPlayground 

let viewController = UIViewController() 
XCPlaygroundPage.currentPage.liveView = viewController.view 

viewController.view.backgroundColor = UIColor.whiteColor() 

let leftLabel = UILabel() 
leftLabel.translatesAutoresizingMaskIntoConstraints = false 
leftLabel.backgroundColor = UIColor.greenColor() 
leftLabel.text = "leftleftleftleftleftleftleftleftleftleftleftleftleftleft" 

let middleLabel = UILabel() 
middleLabel.translatesAutoresizingMaskIntoConstraints = false 
middleLabel.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, forAxis: .Horizontal) 
middleLabel.backgroundColor = UIColor.redColor() 
middleLabel.text = "middle" 

let rightLabel = UILabel() 
rightLabel.translatesAutoresizingMaskIntoConstraints = false 
rightLabel.backgroundColor = UIColor.greenColor() 
rightLabel.text = "right" 

viewController.view.addSubview(leftLabel) 
viewController.view.addSubview(middleLabel) 
viewController.view.addSubview(rightLabel) 

NSLayoutConstraint(item: middleLabel, attribute: .CenterX, relatedBy: .Equal, toItem: viewController.view, attribute: .CenterX, multiplier: 1, constant: 0).active = true 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[leftLabel]->=8-[middleLabel]->=8-[rightLabel]|", options: [.AlignAllTop, .AlignAllBottom], metrics: nil, views: ["leftLabel": leftLabel, "middleLabel": middleLabel, "rightLabel": rightLabel])) 
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[leftLabel]", options: [], metrics: nil, views: ["leftLabel": leftLabel])) 

希望它能帮助:)

+0

谢谢,有什么必要将所有UILabel添加到它自己的UIView? – oldergod

+0

你究竟是什么意思?每个标签都在我自己的视图中... –

+0

为什么不直接将'leftLabel','middleLabel'和'leftLabel'添加到'viewController.view'?你是否需要将每个'xxxxLabel'封装到它自己的'xxxxView'? UILabel扩展UIView,所以我不明白这里的原因。 – oldergod