Ios 将视图顶部与垂直堆栈视图内另一个视图的底部对齐
我有一个标签和图像附加在一个垂直堆栈视图。我希望它们位于堆栈视图的中心,但不要太大。我还希望标签的顶部与图像的底部对齐 我尝试了这些限制: 但是标签会粘贴到堆栈视图的底部 我还尝试:Ios 将视图顶部与垂直堆栈视图内另一个视图的底部对齐,ios,swift,autolayout,constraints,Ios,Swift,Autolayout,Constraints,我有一个标签和图像附加在一个垂直堆栈视图。我希望它们位于堆栈视图的中心,但不要太大。我还希望标签的顶部与图像的底部对齐 我尝试了这些限制: 但是标签会粘贴到堆栈视图的底部 我还尝试: NSLayoutConstraint(item: stackView, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: contentView, attribute: .top, multiplier: 1,
NSLayoutConstraint(item: stackView,
attribute: .top,
relatedBy: .greaterThanOrEqual,
toItem: contentView,
attribute: .top,
multiplier: 1,
constant: 0).isActive = true
NSLayoutConstraint(item: stackView,
attribute: .bottom,
relatedBy: .greaterThanOrEqual,
toItem: contentView,
attribute: .bottom,
multiplier: 1,
constant: 0).isActive = true
NSLayoutConstraint(item: stackView,
attribute: .left,
relatedBy: .greaterThanOrEqual,
toItem: contentView,
attribute: .left,
multiplier: 1,
constant: 0).isActive = true
NSLayoutConstraint(item: stackView,
attribute: .right,
relatedBy: .greaterThanOrEqual,
toItem: contentView,
attribute: .right,
multiplier: 1,
constant: 0).isActive = true
NSLayoutConstraint(item: stackView,
attribute: .centerX,
relatedBy: .equal,
toItem: contentView,
attribute: .centerX,
multiplier: 1,
constant: 0).isActive = true
NSLayoutConstraint(item: stackView,
attribute: .centerY,
relatedBy: .equal,
toItem: contentView,
attribute: .centerY,
multiplier: 1,
constant: 0).isActive = true
NSLayoutConstraint(item: imageView,
attribute: .height,
relatedBy: .equal,
toItem: stackView,
attribute: .height,
multiplier: 0.5,
constant: 0).isActive = true
但现在图像顶部粘在内容视图顶部的顶部,标签底部粘在内容视图底部的底部。A
UIStackView
排列其子视图
在本例中,您有一个UIImageView
和一个UILabel
作为垂直堆栈视图的排列子视图。因此,标签将始终位于图像视图下方
我假设您有其他视图要添加到堆栈视图中——如果是这样
- 要使标签与图像视图“顶部对齐”,请将标签和图像视图嵌入新的
,设置约束以将标签固定到顶部,然后将该UIView
添加到堆栈视图中UIView
- 您可能根本不想使用堆栈视图