Objective c 定义NSLayoutConstraints以在视图中包含矩形
我有以下格式的输入数据:Objective c 定义NSLayoutConstraints以在视图中包含矩形,objective-c,autolayout,Objective C,Autolayout,我有以下格式的输入数据: [ {{0,0}, {0.3, 0.8}}, {{0.4, 0.2}, {0.3, 0.2}} ] 基本上,它们是CGRects的字符串表示,包含在1x1矩形中 现在我需要在屏幕上显示它们,在UIView中,使用自动布局计算尺寸。所以我想把它们转换成NSLayoutConstraints,这样内部矩形就会随着superview的大小而调整大小 我可以很容易地设置子视图的宽度和高度。在数组中所有矩形的循环中,我创建了一个新的UIView: CGRect r =
[
{{0,0}, {0.3, 0.8}},
{{0.4, 0.2}, {0.3, 0.2}}
]
基本上,它们是CGRect
s的字符串表示,包含在1x1
矩形中
现在我需要在屏幕上显示它们,在UIView
中,使用自动布局计算尺寸。所以我想把它们转换成NSLayoutConstraints
,这样内部矩形就会随着superview的大小而调整大小
我可以很容易地设置子视图的宽度和高度。在数组中所有矩形的循环中,我创建了一个新的UIView
:
CGRect r = CGRectFromString(rectString)
UIView *rect = [[UIView alloc] init];
[superview addSubview:rect];
NSLayoutConstraint *width = [NSLayoutConstraint
constraintWithItem: rect
attribute: NSLayoutAttributeWidth
relatedBy: NSLayoutRelationEqual
toItem: superview
attribute: NSLayoutAttributeWidth
multiplier: r.size.width
constant:0];
[superview addConstraint:width];
高度也是如此
但是,当我想将这些矩形的x
和y
坐标设置为基于它们的superview的宽度和高度时,我得到了一个错误
我的代码:
NSLayoutConstraint * left = [NSLayoutConstraint
constraintWithItem: rect
attribute: NSLayoutAttributeLeft
relatedBy: NSLayoutRelationEqual
toItem: superview
attribute: NSLayoutAttributeWidth
multiplier: r.origin.x
constant: 0];
[superview addConstraint: left];
如果我的r.origin.x
是0
,我得到的错误是:乘数为0或零秒的项与第一个属性的位置一起创建了一个等于常数的位置的非法约束。位置属性必须成对指定
在其他情况下(当它不是0
),错误为:布局属性的无效配对
。似乎我无法将nslayouttributeft
和nslayouttributewidth
混合使用
有没有办法这样布置子视图,而不在内部创建复杂的子视图结构(例如,在superview的左/上边缘和矩形的左上角之间添加一个特殊的“间距”视图) 而不是使用乘数将
左侧
约束为宽度
,使用乘数将右侧
约束为高度
,我建议将Right
限制为Right
,乘数为origin.x+size.width
和Bottom
限制为Bottom
,乘数为origin.y+size.height
下面是Swift中的一个实现,它演示了矩形值。使用Swift可以让我在操场上展示结果
let superview = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
superview.backgroundColor = .yellowColor()
let rectStrings = ["{{0,0}, {0.3, 0.8}}", "{{0.4, 0.2}, {0.3, 0.2}}"]
let colors: [UIColor] = [.blueColor(), .redColor()]
for i in 0 ..< rectStrings.count {
let r = CGRectFromString(rectStrings[i])
let rect = UIView()
rect.translatesAutoresizingMaskIntoConstraints = false
rect.backgroundColor = colors[i]
superview.addSubview(rect)
NSLayoutConstraint(item: rect, attribute: .Width, relatedBy: .Equal, toItem: superview,
attribute: .Width, multiplier: r.size.width, constant: 0).active = true
NSLayoutConstraint(item: rect, attribute: .Height, relatedBy: .Equal, toItem: superview,
attribute: .Height, multiplier: r.size.height, constant: 0).active = true
NSLayoutConstraint(item: rect, attribute: .Right, relatedBy: .Equal, toItem: superview,
attribute: .Right, multiplier: r.origin.x + r.size.width, constant: 0).active = true
NSLayoutConstraint(item: rect, attribute: .Bottom, relatedBy: .Equal, toItem: superview,
attribute: .Bottom, multiplier: r.origin.y + r.size.height, constant: 0).active = true
}
superview.layoutIfNeeded()
此答案基于问题中所述1x1矩形中的矩形值。要使用任意大小的矩形,只需除以该矩形的宽度和高度即可 例如,如果矩形是:
let rectStrings = ["{{0,0}, {3.0, 8.0}}", "{{4.0, 2.0}, {3.0, 2.0}}"]
矩形的大小是10宽20高,然后你只需除以宽度和高度来缩放数值
宽度、高度、右侧和底部乘数应为:
r.size.width / 10.0
r.size.height / 20.0
(r.origin.x + r.size.width) / 10.0
(r.origin.y + r.size.height) / 20.0
你能把你想达到的目标写清楚吗?关于左约束和宽度相等是没有意义的-这意味着什么?这都很好,但解决方案不与1x1矩形相关联吗?它使用约束中rect原点和大小的绝对值…@kender,请参阅更新的答案。如果你有一个不是1x1的矩形,你只需要缩放乘数。但是如果我的尺寸也是用自动布局计算的。。。?这样行吗?视图的大小是使用自动布局计算的。我所说的矩形是给出子矩形相对大小的模板。随着“自动布局”调整视图的大小,子视图将按比例增长。
r.size.width / 10.0
r.size.height / 20.0
(r.origin.x + r.size.width) / 10.0
(r.origin.y + r.size.height) / 20.0