[英]How to constrain a UIImageView inside another view?
I have a universal iOS swift project in xcode, and am having trouble getting the sizes of a UIImageView
right. 我在xcode中有一个通用的iOS swift项目,无法正确设置UIImageView
的大小。 The XIB structure is as follows: XIB的结构如下:
View |-- Card (View) |-- CoverImage (UIImageView)
View
is loaded into a UIScrollView
by ViewController.swift
. 通过ViewController.swift
View
加载到UIScrollView
。 Card
has top, bottom, trailing, and leading constraints set up so that there is a 20px 'margin' around it. Card
具有顶部,底部,尾部和前导约束,因此周围有20px的“边距”。 Card
also has rounded corners. Card
也有圆角。 CoverImage
has similar constraints, with top, trailing, and leading being set to superview and bottom being defined in viewDidLoad()
as 40% of the height of Card
. CoverImage
具有相似的约束,顶部,尾部和前导被设置为superview,底部在viewDidLoad()
定义为Card
高度的40%。 The problem is that the width of CoverImage
exceeds the width of Card
, with the excess not being displayed. 问题是CoverImage
的宽度超过了Card
的宽度,而多余的宽度没有显示出来。 As a result, when I try to round the top corners of CoverImage
, only the Top Left corner is rounded. 结果,当我尝试对CoverImage
上角进行CoverImage
,仅对左上角进行了圆角处理。
Setting leadingConstraint.constant
or trailingConstraint.constant
to 0
seems to have no effect, as the top right corner still remains unrounded. 设置leadingConstraint.constant
或trailingConstraint.constant
至0
似乎没有任何效果,因为右上方仍然不圆唇。
How can I set CoverImage
's constraints properly so that it has the correct size? 如何正确设置CoverImage
的约束,使其具有正确的大小? Is it an issue with the constraints I'm setting in the XIB, or is there a better way to define the size of a child view? 我在XIB中设置的约束是否有问题,还是有更好的方法来定义子视图的大小?
For reference, this is the controller swift file for the View containing Card
and CoverImage
: 作为参考,这是包含Card
和CoverImage
的View的控制器快速文件:
class CardViewController: UIViewController {
// MARK: Connect All Subviews/Objects
@IBOutlet weak var card: UIView!
@IBOutlet weak var coverImage: UIImageView!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
@IBOutlet weak var topConstraint: NSLayoutConstraint!
@IBOutlet weak var trailingConstraint: NSLayoutConstraint!
@IBOutlet weak var leadingConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// MARK: Draw Card
card.layer.cornerRadius = 2
card.layer.shadowRadius = 3
card.layer.shadowOpacity = 0.15;
card.layer.shadowOffset = CGSizeMake(1, 1)
card.backgroundColor = UIColor.whiteColor()
// MARK: Draw Cover Image
let imageViewShape : CAShapeLayer = CAShapeLayer()
imageViewShape.bounds = coverImage.frame
imageViewShape.position = coverImage.center
imageViewShape.path = UIBezierPath(roundedRect: coverImage.bounds, byRoundingCorners: [UIRectCorner.TopRight, UIRectCorner.TopLeft], cornerRadii: CGSize(width: 2, height: 2)).CGPath
coverImage.layer.mask = imageViewShape
bottomConstraint.constant = card.frame.height * 0.4
}
}
Thanks to anyone who can help! 感谢任何能提供帮助的人!
First of all, if you try to set the bounds of CAShapeLayer on viewDidLoad
you'll get incorrect dimensions as for the superview isn't rendered on the device at the moment yet; 首先,如果您尝试在viewDidLoad
上设置CAShapeLayer的边界,您将获得不正确的尺寸,因为当前尚未在设备上呈现Superview。 it should be created on viewDidAppear
. 应该在viewDidAppear
上创建它。
Now that we have the correct frame we have to position it properly in superview and by default the mask we append does inherit the constraints of the view it's applied to, thus we have to adjust the origin point of it by 20 pixels on both axis. 现在我们有了正确的框架,我们必须将其正确放置在超级视图中,并且默认情况下,我们附加的遮罩确实继承了其所应用视图的约束,因此我们必须在两个轴上将其原点调整20个像素。
The code I used to make it all look fine as you requested it to: 我用来使它们看起来很不错的代码如您所愿:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
let imageViewShape : CAShapeLayer = CAShapeLayer()
imageViewShape.frame = coverImage.frame
imageViewShape.bounds = CGRect(origin: CGPoint(x: coverImage.bounds.origin.x + 20, y: coverImage.bounds.origin.y + 20), size: coverImage.bounds.size)
imageViewShape.path = UIBezierPath(roundedRect: coverImage.bounds, byRoundingCorners: [UIRectCorner.TopRight, UIRectCorner.TopLeft, UIRectCorner.BottomLeft, UIRectCorner.BottomRight], cornerRadii: CGSize(width: 10, height: 10)).CGPath
coverImage.layer.mask = imageViewShape
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.