- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
任何人都可以深入了解为什么我的代码不起作用吗?
我正在尝试在图片中的 2 个 View (红色和黑色 View )之间添加水平间距,但出于某种原因,我不断收到该错误,我不确定如何修复它。
我假设其他约束正常工作,因为我没有看到任何问题,无论是在模拟中还是在控制台中。
用户请求的代码:https://gist.github.com/sungkim23/66ecd9ce71d0480f083bcd05caf0a58c
import UIKit
class BuyButton: UIView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//MARK: - View
backgroundColor = UIColor(red: 180/255, green: 35/255, blue: 115/255, alpha: 1)
layer.cornerRadius = 3
//MARK: - Price View
let priceView = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: bounds.size.height))
priceView.backgroundColor = UIColor.black
addSubview(priceView)
//MARK: - Purchase View
let purchaseView = UIView(frame: CGRect(x: 80, y: 0, width: bounds.size.width - 80, height: bounds.size.height))
purchaseView.backgroundColor = UIColor.red
addSubview(purchaseView)
//MARK: - Price Label
let priceLabel = UILabel(frame: CGRect.zero)
priceLabel.text = "$ 50.00"
priceLabel.textAlignment = .center
priceLabel.backgroundColor = .red
priceLabel.translatesAutoresizingMaskIntoConstraints = false
priceView.addSubview(priceLabel)
//MARK: - Purchase Label
let purchaseLabel = UILabel(frame: CGRect.zero)
purchaseLabel.text = "Purchase"
purchaseLabel.textAlignment = .center
purchaseLabel.backgroundColor = .green
purchaseLabel.translatesAutoresizingMaskIntoConstraints = false
purchaseView.addSubview(purchaseLabel)
//MARK: - View Constraints
priceView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
priceView.topAnchor.constraint(equalTo: topAnchor).isActive = true
priceView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
purchaseView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
purchaseView.topAnchor.constraint(equalTo: topAnchor).isActive = true
purchaseView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
priceView.centerXAnchor.constraint(equalTo: purchaseView.centerXAnchor).isActive = true
//MARK: - Label Constraints
priceLabel.centerXAnchor.constraint(equalTo: priceView.centerXAnchor).isActive = true
priceLabel.centerYAnchor.constraint(equalTo: priceView.centerYAnchor).isActive = true
purchaseLabel.centerXAnchor.constraint(equalTo: purchaseView.centerXAnchor).isActive = true
purchaseLabel.centerYAnchor.constraint(equalTo: purchaseView.centerYAnchor).isActive = true
//MARK: - Constraint priorities
}
}
更新 1 - 根据要求向问题添加错误
2017-03-13 11:02:38.440892 ButtonTest[20954:1645749] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x60000009a400 h=--& v=--& UIView:0x7fe55be017b0.midX == 40 (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x600000099ff0 h=--& v=--& UIView:0x7fe55bc09310.midX == 232 (active)>",
"<NSLayoutConstraint:0x600000099c30 UIView:0x7fe55be017b0.centerX == UIView:0x7fe55bc09310.centerX (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000099c30 UIView:0x7fe55be017b0.centerX == UIView:0x7fe55bc09310.centerX (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
更新 2 - tl;dr
使用
yourView.leadingAnchor.constraint(equalTo: yourOtherView.trailingAnchor).isActive = true
代替
yourView.centerXAnchor.constraint(equalTo: yourOtherView.centerXAnchor).isActive = true
最佳答案
好的,根据您对马特问题的评论。你仍然没有在约束中思考 :D
您不需要考虑 bounds.size.width - 80
,它是 frame
和 rect
的东西。这不是要考虑的 AutoLayout
事情。
为了考虑 AutoLayout 中的布局,您需要向应用描述布局。
这就是我想你想要的......
在此列表中,价格 View 和购买 View 只有一种可能的布局。任何其他布局都会与约束相矛盾。另外,我没有添加任何我不需要的东西。例如,按照这些说明,两个 View 的 centerYAnchor
将相同。我不需要明确添加。
现在您有了这个非常简单的描述...只需添加即可。
// set the width of price view
priceView.widthAnchor.constraint(equalToConstant: 80).isActive = true
// now add the other edges to super view
priceView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
priceView.topAnchor.constraint(equalTo: topAnchor).isActive = true
priceView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
// set the spacing between price view and purchase view
purchaseView.leadingAnchor.constraint(equalTo: priceView.trailingAnchor).isActive = true
// add the other edges to the super view
purchaseView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
purchaseView.topAnchor.constraint(equalTo: topAnchor).isActive = true
purchaseView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
应该是这样(对于价格 View 和购买 View )。
在使用 AutoLayout 时不要试图使您的任务过于复杂。 AutoLayout 为您完成了很多工作。
编辑 - UIStackView
根据@abizern 对您的问题的评论。您真的应该检查一下 UIStackView
。它消除了很多这种痛苦,并使使用自动布局的布局变得更加容易。
关于ios - 布局水平 anchor 问题 - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42767334/
当用户将鼠标悬停在 anchor 上时,除了悬停的 anchor 之外的每个 anchor 都会改变颜色。假设有 link1 link2 link3。如果用户将鼠标悬停在 link1 上,我希望更改
'; echo "".$value['eventsearchresultwhat'].""; echo "\n"; echo $value['eventsearchr
我有几个多列下拉菜单作为我的主导航的一部分。由于某种原因,两个多列下拉菜单中每个链接周围的 anchor 命中区域位于链接本身上方。我该如何修复它,使点击区域像普通 anchor 一样以链接文本为中心
我正在 android studio 中创建内部构建 AR 导航。我正在寻找一种方法将 anchor 与其他 anchor 或 anchor 节点/节点“连接”起来。我不确定使用哪一个。基于我将强制用
我有很多 anchor 可以在 doxygen 中描绘,例如 \anchor pic_foo \image html foo.gif "My Caption" \anchor pic_bar \ima
我有 3 个 anchor block ,当我加载页面时,我需要 3 个不同的随机 background-colors。然而,只有第一个 anchor 随机改变 background-color。这是
我有一个带有 float 顶部导航栏的网页。我想将 anchor 链接添加到目录以快速定向到页面上的其他位置。 #top { display: flex; z-index: 100; ba
我无法理解为什么将 text-align 属性设置为 right 值,将 span 标签移出 anchor 标签区域(溢出)? HTML文件的结构 a.btn{ border: 1px solid
我需要使用 Jquery 将类“Selected”添加到一个 anchor ,捕获它的唯一方法是通过其属性“active=true”。然后,当用户选择其他 anchor 时,新 anchor 将被设置
我在 hubspot 上有一个基于搜索词查询动态生成的搜索列表页面。 HTML结构如下: a class="hs-search-results__title" href="www.somethi
标题隐晦,但很难解释。使用 Firefox,前往 http://images.google.com进行随机搜索,然后将第一个搜索结果拖到地址栏。您会看到它转到具有图像 anchor 的页面。然后单击“
我正在为 vue-router 不滚动/导航到 anchor 标签(例如:#anchor)而苦苦挣扎。我在 Stack Overflow 上阅读了各种解决方案,但到目前为止都没有奏效。 请在下面找到我
我正在制作 AR 应用程序。要求很简单。在“配置阶段”,我启动应用程序并将一个立方体放在房间的地板上,然后关闭它。稍后当我再次启动应用程序时,当相机面向同一楼层时,立方体应该留在那里。根据我发现的信息
我正在 WordPress 中通过简码展示一组产品。显示屏上有图像和按钮。 问题:只有照片包含指向单个产品页面的链接。关联的按钮没有指向单个产品页面的链接。 这是当前代码:
我正在尝试做一件奇怪的事情。我将解释一般情况,然后按照我想要执行的步骤进行。我有一个 内的项目列表。当我按下一个元素时,必须打开一个模式。在这个模态中,有与 相同的元素。但详细解释了。当模式打开时
尝试从 anchor 克隆文本并将其设置为同一 anchor 的标题属性,但不了解如何使用正确语法的逻辑。 $(document).ready(function () { $('.desc a').c
--- #File A - Lunch: &01 - Chicken - Rice - Sallad ... --- #File B - Mon
我的用例是: 点击屏幕并将“点”保存为起始 anchor 第二次点击屏幕并将“点”保存为结束 anchor 按下按钮将对象从开始 anchor 移动到结束 anchor 我已经构建了自己的节点,该节点
我的菜单和基于#anchors 的导航有问题。我正在使用 JS 函数在滚动时将菜单置于顶部(#stickyheader)。我的菜单上有一个 div,滚动时会消失(#unstickyheader)。该
我目前有这个基础页面淡入淡出脚本函数,当前针对所有 anchor ('a'),但不确定如何准确排除具有类(.noFade)的某个 anchor 淡出。 JS: $(document).ready(fu
我是一名优秀的程序员,十分优秀!