- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码用于处理高度和宽度约束。尝试添加前导约束和顶部约束时它会崩溃。我还有其他几组按钮,它们具有高度、宽度、前导、顶部约束,但它们都是在 Storyboard上设置的,所以我认为这是我必须添加到每个按钮的唯一 4 个约束。
我有 7 个按钮,每个按钮代表一周中的一天。当我添加代码来执行前导和顶部约束时,它会因下面的错误代码而中断。仅使用高度/宽度即可正常工作。我猜这与我添加 subview 的方式或按钮与 View Controller 的关系有关,或者以编程方式执行时我需要的不仅仅是我一直在使用的 4 个约束(前导、顶部、宽度、高度)在 Storyboard上。
func setupWeekdayButtons() {
self.weeklyButtons = [self.mondayButton, self.tuesdayButton, self.wednesdayButton, self.thursdayButton, self.fridayButton, self.saturdayButton, self.sundayButton]
for i in 0...6 {
print(i)
self.weeklyButtons[i].translatesAutoresizingMaskIntoConstraints = false
self.weeklyButtons[i].setTitle(self.weekdayLabels[i], for: .normal)
self.weeklyButtons[i].layer.borderColor = UIColor.black.cgColor
self.weeklyButtons[i].titleLabel?.textAlignment = .center
self.weeklyButtons[i].layer.borderWidth = 1.0
self.weeklyButtons[i].layer.cornerRadius = 6.0
self.weeklyButtons[i].setTitleColor(.black, for: .normal)
self.weeklyButtons[i].setTitleColor(.red, for: .selected)
self.weeklyButtons[i].addTarget(self, action: #selector(selectedDailyButton), for: .touchUpInside)
self.view.addSubview(self.weeklyButtons[i])
let heightConstraint = NSLayoutConstraint(
item: self.weeklyButtons[i],
attribute: NSLayoutConstraint.Attribute.height,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: nil,
attribute: NSLayoutConstraint.Attribute.notAnAttribute,
multiplier: 1.0,
constant: 30
)
let widthConstraint = NSLayoutConstraint(
item: self.weeklyButtons[i],
attribute: NSLayoutConstraint.Attribute.width,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: nil,
attribute: NSLayoutConstraint.Attribute.notAnAttribute,
multiplier: 1.0,
constant: 30
)
let leadingConstraint = NSLayoutConstraint(
item: self.weeklyButtons[i],
attribute: NSLayoutConstraint.Attribute.leading,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: self.view,
attribute: NSLayoutConstraint.Attribute.leading,
multiplier: 1.0,
constant: 100
)
let topConstraint = NSLayoutConstraint(
item: self.weeklyButtons[i],
attribute: NSLayoutConstraint.Attribute.top,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: self.view,
attribute: NSLayoutConstraint.Attribute.top,
multiplier: 1.0,
constant: 100
)
self.weeklyButtons[i].addConstraint(heightConstraint)
self.weeklyButtons[i].addConstraint(widthConstraint)
self.weeklyButtons[i].addConstraint(leadingConstraint)
self.weeklyButtons[i].addConstraint(topConstraint)
}
}
2019-03-07 14:38:59.176638-0500 Daily[27852:1408014] [LayoutConstraints] The view hierarchy is not prepared for the constraint: When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2019-03-07 14:38:59.177816-0500 Daily[27852:1408014] [LayoutConstraints] View hierarchy unprepared for constraint. Constraint: Container hierarchy: > | > View not found in container hierarchy: > That view's superview: NO SUPERVIEW
2019-03-07 14:38:59.192176-0500 Daily[27852:1408014] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint: view:>'
最佳答案
self.weeklyButtons[i].addConstraint(leadingConstraint)
约束涉及weeklyButtons[i]
和self.view
。如果您使用 addConstraint 激活约束,则必须将约束添加到两个 View 的共同祖先。这就是此错误消息告诉您的内容:
When added to a view, the constraint's items must be descendants of that view (or the view itself).
由于 self.view
是 weeklyButtons[i]
的 super View ,因此它被视为两个 View 的共同祖先。因此,在这种情况下,您可以将约束添加到 self.view
中:
self.view.addConstraint(leadingConstraint)
但不要这样做。从 iOS 8 开始,您可以直接激活约束,UIKit 会自动将其添加到正确的 View 中:
leadingConstraint.isActive = true
但不要这样做。由于您连续添加四个约束,因此一次激活四个约束可能会稍微更有效,如下所示:
NSLayoutConstraint.activate([
leadingConstraint,
topConstraint,
widthConstraint,
heightConstraint])
但不要这样做。自 iOS 9 以来,出现了一种更易读的方式来创建约束:
NSLayoutConstraint.activate([
self.weeklyButtons[i].heightAnchor.constraint(equalToConstant: 30),
self.weeklyButtons[i].widthAnchor.constraint(equalToConstant: 30),
self.weeklyButtons[i].leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 100),
self.weeklyButtons[i].topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
])
但不要这样做。使用 for
/in
循环代替索引变量 i
:
func setupWeekdayButtons() {
weeklyButtons = [mondayButton, tuesdayButton, wednesdayButton, thursdayButton, fridayButton, saturdayButton, sundayButton]
for (button, title) in zip(weeklyButtons, weekdayLabels) {
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(title, for: .normal)
button.layer.borderColor = UIColor.black.cgColor
button.titleLabel?.textAlignment = .center
button.layer.borderWidth = 1.0
button.layer.cornerRadius = 6.0
button.setTitleColor(.black, for: .normal)
button.setTitleColor(.red, for: .selected)
button.addTarget(self, action: #selector(selectedDailyButton), for: .touchUpInside)
view.addSubview(button)
NSLayoutConstraint.activate([
button.heightAnchor.constraint(equalToConstant: 30),
button.widthAnchor.constraint(equalToConstant: 30),
button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 100),
button.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
])
}
}
这样做。
关于ios - 以编程方式添加前导/顶部约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55051717/
我目前有以下选择语句: SELECT CONCAT(TRUNCATE(HOUR("215:00:36")/24,0),":",HOUR("215:00:36")%24,":",MINUTE("215:
我正在尝试将字符串编码为 base36。 static char *decode(unsigned long long value) { char base36[37] = "01234567
如果我有一个输入字符串,input 有三个或更多小数位,我希望该字符串由单独的 if-loop 处理。 我为此目的创建了以下控制台程序,如果(第一个约束)有小数点 .,则输入 if-loop字符串和(
我有一小段代码,它有一个整数数组,其中包含一些前导 0 的元素。 int arr[]={012,234,071}; cout<
如何从强制范围中删除前导,以便 Some field Any 1 2 3 « 最佳答案 删除 vertical-align: middle 后我觉得不错。 .mandato
我有这个 psd 并试图将它转换为 html css。 但是我无法从psd中计算出css中的行高。 如何根据前导+字体大小计算行高? 谢谢 最佳答案 如果行距不是自动的:LineHeight = fo
有谁知道如何更改文本字段上的行高(行距),最好是在 Interface Builder 中? 我已经找了大约一个小时了,但没有找到。 最佳答案 解决方法如下: Resizing NSTextField
是否可以在 C# 中的单元格元素(行)之间添加空格?我正在 Visual Studio 2012 中创建 pdf,并希望在行之间设置一些空间。我有这样的东西: PdfPTable cellTable
是否可以在 C# 中的单元格(行)元素之间添加空格?我正在 visual studio 2012 中创建一个 pdf,并希望在行之间设置一些空间。我有这样的东西: PdfPTable cellTabl
type ApplyFunc func(commitIndex uint64, cmd []byte) []byte 对于这个声明。我的理解是,这是一个函数指针。它的名字是 ApplyFunc。并且此
我很好奇多列索引的前导列的概念。 我正在使用这个示例 dvdrental分贝。 这是查询: SELECT title, length, rating, replacement_cost
我想将一个 double 格式格式化为最大长度为7的字符串,其中包含一个点“.”以及其后一位数字。 例如: 123.4 becomes "00123.4" 12345 becomes "12345
我需要正则表达式的帮助来从字符串中去除不需要的字符(在 Java 中)。我用 4 个正则表达式解决了这个问题。替换将被多次调用 [peeks: 50+ times/sec] 它并降低性能。但我认为它肯
只是将两个值连接到 mysql 表中的一列。 mysql 表中的第一个值和 PHP 常量中的第二个值保存为 define('ACCOUNTCODESUFFIX','016');。 MySql 查询有点
我如何按此顺序返回这些: 1: aaaa 2: bbbb 3: the cccc 4: dddd 所以忽略开头的“the” 目前我正在使用。 select * from houses order by
我通常使用以下代码将 PHP 变量导入 JavaScript: echo("var zipCode = " . $zipCode . ";"); 这工作正常,但对于以 0 开头的邮政编码,它会被 ch
有什么方法可以用前导 0 和小数来格式化 NSNumber 吗?例如,我需要有能力写 4.5 和 000。目前我有它允许小数,但不允许前导 0。 NSNumberFormatter *f = [[NS
你好我正在以编程方式创建标签栏,在我的功能中需要选择填充自定义颜色的标签,到目前为止我已经使用下面的代码实现了这一点,下面的输出见截图。 tabBarController.tabBar.selecti
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Integer with leading zeroes 有人能告诉我这是怎么回事吗?当我用前导零初始化一个
在 Xcode 6 中,我可以使用编辑器菜单来固定: 引领空间至超视 到 Superview 的尾随空间 顶部空间到Superview Superview的底部空间 我找不到在 Xcode 7 bet
我是一名优秀的程序员,十分优秀!