- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想将 subview 的中心限制在父 View 总宽度的 1/6。
例如:
如果父 View 的宽度 = 6
subview 的 CenterX = 1
我在 superview 类(self)中写了以下代码来约束 aSubview 的 centerX,它崩溃了:
// Hits here
[NSLayoutConstraint constraintWithItem:self.aSubview
attribute:NSLayoutAttributeCenterX
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:1/6
constant:0];
// Crashes here
有没有办法用 NSLayoutConstraints 做到这一点?
最佳答案
我有两个想法为什么这可能会崩溃:
1) 在这种情况下,self 是什么?您确定它是 UIView 的子类吗?
2) 1/6 应为 0,这不是有效的乘数。试试 1.0/6
更新:
方法名是
+ constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:
代码中的 relateBy: 部分在哪里?
更新 2:
看来终究是不允许的。我试图重现崩溃,但它记录了以下错误:
Invalid pairing of layout attributes
但是!您可以使用 Trailing 而不是 Width 来实现所需的布局,如果 superview 的左侧连接到屏幕,它实际上具有相同的值(参见图像以更好地理解)。
这已经过测试并且可以正常工作:
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.aView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0/2
constant:0];
[self.view addConstraint:constraint];
请务必将您的约束添加到您要定位的 View 的父 View 中。
关于ios - 将 View 的 CenterX 限制为父 View 总宽度的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31389137/
我一直在使用定制的 PannaableCanvas was presented here ,我遇到了一些麻烦。我的软件即将完成,我需要做的最后一件事是添加对在屏幕中心生成节点的支持。所以我最初做的是:
我正在尝试为我的 ListView 项目背景制作渐变效果。因此,我创建了一个新的可绘制 XML,其中包含以下内容: 但 centerX 属性似乎不起作用,因为 centerX 点仍处于 50%。如
我有一个 UITableViewController,如果没有任何内容可显示,它不会显示任何部分。我添加了一个标签来向用户表明此代码没有任何可显示的内容: label = UILabel(frame:
我有一个 subview 以编程方式由其父 View 呈现。我想将 subview 的 CenterXAnchor 设置为受限于父 View 的 CenterXAnchor,但我希望 subview
我读过一个旋转变换,该变换支持使用 rotate(angle, centerX, centerY) 围绕中心点旋转,但它在通过 CSS 应用时似乎不起作用。 它作为一个属性内联工作:
我正在尝试使用 Cartography 为 UILabel 设置动画并使用此代码: let group = ConstraintGroup() constrain(alertLabel
我目前可以将 View 的 centerX 限制在其 super View 的左侧,如下所示: NSLayoutConstraint *con = [NSLayoutConstraint constr
我想将 subview 的中心限制在父 View 总宽度的 1/6。 例如: 如果父 View 的宽度 = 6 subview 的 CenterX = 1 我在 superview 类(self)中写
我是一名优秀的程序员,十分优秀!