- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在我的 View Controller 的 updateViewConstraints()
中的 UIImgeView 实例上设置 .Width 和 .Top NSLayoutConstraints。在 XCode 可视化调试器中,我可以看到约束实际上已设置,但由于某种原因它们未被使用(?)它们显示为未(内容大小)并且显示为灰色。当我使用 NSLog 输出约束时,我也可以看到它们已设置,但不知何故图像保持原始大小...... 这是什么意思?
constraints in parent: [<NSLayoutConstraint:0x7b379590 UIImageView:0x7af897b0.centerX == UIView:0x7b37d2e0.centerX>,
<NSLayoutConstraint:0x7b36e3d0 V:|-(0)-[UIImageView:0x7af897b0] (Names: '|':UIView:0x7b37d2e0 )>]
constraints in view: [<NSContentSizeLayoutConstraint:0x7ae892b0 H:[UIImageView:0x7af897b0(301)] Hug:251 CompressionResistance:750>,
<NSContentSizeLayoutConstraint:0x7ae834e0 V:[UIImageView:0x7af897b0(365)] Hug:251 CompressionResistance:750>]
设置约束的代码:
/// Image view with the character + ellipse
@IBOutlet weak var charImageView: UIImageView!
override func updateViewConstraints() {
super.updateViewConstraints()
charImageView.widthLayoutConstraint?.constant = 301.0
charImageView.heightLayoutConstraint?.constant = 365.0
}
我用来获取 widthLayoutConstraint
和 heightLayoutConstraint
的扩展
extension UIView{
/**
Gets the width layout constraint defined as the NSLayoutConstraint whose first item is self and first attribute is Height
*/
var heightLayoutConstraint:NSLayoutConstraint? {
get{
for constraint in constraints {
if constraint.firstItem as? UIView == self &&
constraint.firstAttribute == NSLayoutAttribute.Height
{
return constraint
}
}
return nil
}
}
/**
Gets the width layout constraint defined as the NSLayoutConstraint whose first item is self and first attribute is Width
*/
var widthLayoutConstraint:NSLayoutConstraint? {
get{
for constraint in constraints {
if constraint.firstItem as? UIView == self &&
constraint.firstAttribute == NSLayoutAttribute.Width
{
return constraint
}
}
return nil
}
}
}
这是最初使用 IB 添加布局约束的方式。如您所见,它们是为 Compact Width size Class 添加的,我在看到问题时正在使用它(iPhone4S 模拟器)。
约束已经通过 IB 添加,我正在用代码更新它们的常量。调试器清楚地显示常量实际上已更新,但不知何故图像以原始大小绘制。
最佳答案
灰色约束表示这些约束存在但未安装在您的 View 中。
引用下图:
Installed
选项更新:
您可以使用以下代码添加width
约束:
self.addConstraint(NSLayoutConstraint(
item: charImageView,
attribute: .Width,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 1.0,
constant: 301.0))
您可以使用我的其他答案更新约束值:
关于ios - UIImageView 上的宽度和高度 NSLayoutConstraints 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37592337/
我正在尝试将 UIView 添加到 UITableView 的标题,然后,使用 NSLayoutConstraints 我想给它一个高度。 我已经浏览了 Apple Docs 和 WWDC 2012
我正在使用 Xcode 9 beta 5 。我务实地添加了反向操作 addConstraint(NSLayoutConstraint.constraints(withVisualFormat: "H:
我使用的是 XCode 版本 9.0 (9A235)、macOS Sierra 版本 10.12.6 (16G29)、iOS 11.0。 我需要将约束添加到数组中,然后我需要激活它。 为此我确实喜欢.
我正在通过代码构建 UICollectionViewCell 界面。一切正常,除了正确的约束。当我运行应用程序时,标题标签的右边距为零。这是我的代码 let titleLabel: UILabel =
我的代码中有两个类,一个是NameCell ,其中包含一个带有文本的简单 UILabel。第二个是NameValueCell ,它继承自该类,但还添加了属性 UIView *valueView . 需
我正在尝试设置一个面板来显示一些信息。我无法让自动布局按我的意愿工作。 这就是我想要做的事情: 我有一个 headerView、一个 UIImageView 和 5 个标签。我希望标签垂直排列,我希望
各位同事, 我开发了一个自定义键盘。键盘类型(字母、数字、特殊字符)之间的切换速度存在问题。这是由于每次重新绘制按钮时的事实。 NSLayoutConstraint我设置如下: 有一个类Keyboar
我试图对 View 内的标签 -> dateLabel 施加约束,这些是我给出的约束: let horizontalConstraint = NSLayoutConstraint(item: date
我使用编程约束将 View 定义为 iOS 屏幕上的弹出窗口。 let stopTimer = StoppageTimer(frame: CGRect.zero) 该 View 本身包含一个堆
我想将 2 个按钮移动到屏幕宽度的中心。它应该看起来像: |SecondButton(100)->| 我从第一个按钮开始。 var const = NSLayoutConstraint(item: f
这是我第一次尝试使用约束,所以请原谅这个初级问题。 我希望能够设置一系列约束,以便 subview 小于 View 宽度的 75% 或 View 宽度的 75%。因此,如果我的 subview 的宽度
我有一个在容器中的 View 在我放置在这个容器中的一些 View Controller 中,我想要全局边距 但我不知道如何将它们添加到容器中。 我的意思是我不知道如何定义 NSLayoutConst
我正在尝试通过 for 循环创建 UILabel,该循环从 NSArray 获取其 NSInteger。循环工作正常,但我看不到所有顶点一个接一个。我肯定是有约束的错误。你能帮帮我吗? 根据我发布的这
我在 UIView 中有 2 个 UILabel: [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat
我下面的代码使用代码无 Storyboard 来显示对象。该项目已构建,但我看不到我试图以编程方式放置的文本字段。我不知道为什么代码没有出现。我试图不使用 Storyboard。 var enter
我在 Web View 和工具栏之间添加约束时遇到问题。我正在使用以下代码但出现错误。 NSDictionary *viewsDictionary6 = NSDictionaryOfVariableB
我有一个自定义 UITableViewCell,我正在尝试自动布局 UILabel,因为它的文本可以有任意长度,但我收到此错误: Break on objc_exception_throw to ca
我有一个包含 UIWebview 的 UIView。 UIView 具有前导和尾随约束 (20pt)。现在,如果 UIWebview 放大,我通过 UIScrollViewDelegate:scrol
我有一个程序化的 UIToolBar,它是在我的某个 ViewController 的 viewWillAppear: 方法中创建和绘制的。我有 NSLayoutConstraint 设置以将我的 t
我有一个自定义的 UITableViewCell。该单元具有三个标签。最左边的“金额”标签具有以下约束。 在单元格的右侧,我有另一个标签,“Label Dollar Amount”。它具有以下约束:
我是一名优秀的程序员,十分优秀!