ios - 无法使用 PureLayout 将标签定位到 View 的右边缘

标签 ios swift autolayout pure-layout

我发现了一个很棒的教程,它很好地解释了如何使用 PureLayout 来做一些很酷且简单的布局工作。我在让标签与我试图将其放入的 View 右侧对齐时遇到了一些麻烦

我在类(class)中名列前茅

var cardView: UIView!
var clipView: UIView!
var scrollView: UIScrollView!

...

然后我将其放在 viewDidLoad()

var openNowLabelView: UIView!
var openNowLabel: UILabel!

cardView = UIView(frame: CGRect.zero)
cardView.layer.shadowColor = UIColor.black.cgColor
cardView.layer.shadowOffset = CGSize(width: 0, height: 12)
cardView.layer.shadowOpacity = 0.33
cardView.layer.shadowRadius = 8
cardView.layer.shadowPath = UIBezierPath(roundedRect: cardView.bounds, cornerRadius: 12).cgPath

self.addSubview(cardView)

clipView = UIView(frame: CGRect.zero)
clipView.backgroundColor = UIColor(red: 42/255, green: 46/255, blue: 61/255, alpha: 1)
clipView.layer.cornerRadius = 12.0
clipView.clipsToBounds = true

cardView.addSubview(clipView)

scrollView = UIScrollView(frame: CGRect.zero)

clipView.addSubview(scrollView)

...

openNowLabelView = UIView(frame: CGRect.zero)
openNowLabel = UILabel(frame: CGRect.zero)
openNowLabel.textColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
openNowLabel.font = UIFont(name: "Gibson-Regular", size: 18)
openNowLabel.text = "Open Now"
openNowLabelView.layer.borderWidth = 1
openNowLabelView.layer.borderColor = UIColor.red.cgColor
openNowLabel.textAlignment = .right

openNowLabelView.addSubview(openNowLabel)
scrollView.addSubview(openNowLabelView)

我有一个覆盖 func updateConstraints() 函数

if(shouldSetupConstraints) {
  cardView.autoPinEdge(toSuperviewEdge: .bottom, withInset: edgesInset)
  cardView.autoPinEdge(toSuperviewEdge: .left, withInset: edgesInset)
  cardView.autoPinEdge(toSuperviewEdge: .right, withInset: edgesInset)
  cardView.autoSetDimension(.height, toSize: UIScreen.main.bounds.height - 32)

  clipView.autoPinEdge(.top, to: .top, of: cardView)
  clipView.autoPinEdge(.left, to: .left, of: cardView)
  clipView.autoPinEdge(.right, to: .right, of: cardView)
  clipView.autoPinEdge(.bottom, to: .bottom, of: cardView)

  scrollView.autoPinEdge(.top, to: .top, of: clipView, withOffset: 161)
  scrollView.autoPinEdge(.bottom, to: .bottom, of: clipView)
  scrollView.autoPinEdge(.left, to: .left, of: clipView)
  scrollView.autoPinEdge(.right, to: .right, of: clipView)

  ...

  openNowLabel.sizeToFit()
  openNowLabelView.autoSetDimension(.height, toSize: openNowLabel.bounds.height)
  openNowLabelView.autoSetDimension(.width, toSize: openNowLabel.bounds.width)
  openNowLabelView.autoPinEdge(.right, to: .right, of: scrollView, withOffset: edgesInset)
  openNowLabelView.autoPinEdge(.top, to: .top, of: placeDistanceLabelView)
  shouldSetupConstraints = false
}
super.updateConstraints()

这是我得到的结果,看起来像这样......

enter image description here

我不明白为什么会发生这种情况

更新

我已经清理了一些 View 和标签,以使其更清晰,我能够解决我遇到的问题,我需要在 View 中嵌入标签以制作 PureLayout 库工作,希望这可以更好地说明我的代码与我在屏幕上看到的内容的关系

// setup views and labels
...
var scrollView: UIScrollView = {
    let view = UIScrollView.newAutoLayout()
    view?.translatesAutoresizingMaskIntoConstraints = false
    return view!
}()
...
var placeTitleLabel: UILabel = {
    let label = UILabel.newAutoLayout()
    label?.numberOfLines = 1
    label?.lineBreakMode = .byClipping
    label?.textColor = .white
    label?.font = UIFont(name: "Gibson-Semibold", size: 25)
    return label!
}()
var placeDistanceLabel: UILabel = {
    let label = UILabel.newAutoLayout()
    label?.numberOfLines = 1
    label?.lineBreakMode = .byClipping
    label?.textColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
    label?.font = UIFont(name: "Gibson-Regular", size: 18)
    return label!
}()
var placeNumReviewsLabel: UILabel = {
    let label = UILabel.newAutoLayout()
    label?.numberOfLines = 1
    label?.lineBreakMode = .byClipping
    label?.textColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
    label?.font = UIFont(name: "Gibson-Regular", size: 12)
    return label!
}()
var openNowLabel: UILabel = {
    let label = UILabel.newAutoLayout()
    label?.numberOfLines = 1
    label?.lineBreakMode = .byClipping
    label?.textColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
    label?.font = UIFont(name: "Gibson-Regular", size: 18)
    label?.text = "Open Now"
    label?.textAlignment = .right
    return label!
}()

// constraints
placeTitleLabel.setContentCompressionResistancePriority(UILayoutPriority.required, for: .vertical)
placeTitleLabel.autoPinEdge(.top, to: .top, of: scrollView, withOffset: edgesInset)
placeTitleLabel.autoPinEdge(.left, to: .left, of: scrollView, withOffset: edgesInset)

placeDistanceLabel.setContentCompressionResistancePriority(UILayoutPriority.required, for: .vertical)
placeDistanceLabel.autoPinEdge(.left, to: .left, of: scrollView, withOffset: edgesInset)
placeDistanceLabel.autoPinEdge(.top, to: .bottom, of: placeTitleLabel, withOffset: edgesInset / 2)

placeNumReviewsLabel.setContentCompressionResistancePriority(UILayoutPriority.required, for: .vertical)
placeNumReviewsLabel.autoPinEdge(.left, to: .right, of: ratingView, withOffset: edgesInset)
placeNumReviewsLabel.autoPinEdge(.top, to: .top, of: ratingView, withOffset: -2)

openNowLabel.setContentCompressionResistancePriority(UILayoutPriority.required, for: .vertical)
openNowLabel.autoPinEdge(.trailing, to: .trailing, of: scrollView, withOffset: edgesInset)
openNowLabel.autoPinEdge(.top, to: .top, of: placeDistanceLabel)

据我在检查器中可以看出,当我突出显示 scrollView 时,嵌入其中的标签具有容器的完整宽度,并且标签本身似乎具有宽度等,我只是不知道为什么我无法对齐右边缘,左边缘没有问题。

当我检查模拟器时,我在 openNowLabel 旁边看到这个图标,但我不确定它的含义或如何获取有关它的信息,这是否与我的问题有关?

enter image description here

这里是我上传的存储库的链接,其中包含运行所需的所有 pod,并查看我所看到的内容是否有助于理解原始上下文

https://github.com/Jordan4jc/place-app-test

最佳答案

从您共享的代码示例来看,您似乎添加了 openNowLabel 作为 openNowLabelView 的 subview ,但您从未设置其约束。

如果我正确理解您想要实现的目标,您需要将 openNowLabel 的边缘固定到其 super View (在本例中为 openNowLabelView)。

更新

根据您共享的项目,看起来您的 ScrollView 及其 subview 没有获得所有所需的约束,因此系统知道如何计算内容大小。看看 Apple 的这篇文章 ( Technical Note TN2154 ):

Use constraints to lay out the subviews within the scroll view, being sure that the constraints tie to all four edges of the scroll view and do not rely on the scroll view to get their size.

尝试添加这些约束并查看是否获得所需的布局:

openNowLabel.autoPinEdge(.left, to: .right, of: placeDistanceLabel, withOffset: edgesInset)
ratingView.autoPinEdge(.bottom, to: .bottom, of: scrollView, withOffset: edgesInset)

注意:如果您只添加第一个约束,您的布局将看起来相同,但 ScrollView 的布局将不明确,因为它将无法计算内容大小的高度。

关于ios - 无法使用 PureLayout 将标签定位到 View 的右边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50298183/

相关文章:

swift - DispatchQueue 中的目标参数

ios - fatal error : use of unimplemented initializer 'init(coder:)' for class

iphone - 保护 iPhone (iOS)/服务器通信

swift - 如何修复 "Optimization Opportunities"

ios - 自动布局约束差异

ios - 如何以编程方式分配 UIView 自动布局边缘以匹配 super View ?

ios - 如何处理代码中的 NSLayoutConstraint 冗长问题?

ios - 从 TabViewController 中的 NavigationController 返回上一个 ViewController

ios - 如何设置: 2015-06-17 05:25:21 +0000 as a let NSDate constant

iOS 9 - 网络可达性变化导致 View 卡住 - 不是崩溃