- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是布局。顶部的标签文本可能会变得很长,我希望在底部选择的标签此刻有足够的空间时距离底部按钮至少 40 pt,而当空间不足时至少 10 pt。尝试优先排序,但似乎不起作用,请帮助我。这是我的代码的一部分。
buttonView.snp.updateConstraints { (make) in
make.leading.bottom.trailing.equalToSuperview()
make.height.equalTo(buttonView.intrinsicContentSize.height)
}
contentLabel.snp.updateConstraints { (make) in
make.top.greaterThanOrEqualTo(headerImageView.snp.bottom).offset(10.dp)
make.leading.equalTo(50.dph)
make.trailing.equalTo(-50.dph)
}
codeTextField.snp.updateConstraints { (make) in
make.top.equalTo(contentLabel.snp.bottom).offset(10.dp)
make.leading.equalTo(46.5.dp)
make.trailing.equalTo(-46.5.dp)
make.height.equalTo(50.dp)
}
tipsLabel.snp.updateConstraints { (make) in
make.top.equalTo(codeTextField.snp.bottom).offset(10.dp)
make.width.centerX.equalTo(codeTextField)
make.bottom.equalTo(buttonView.snp.top).offset(-10.dp)
make.bottom.greaterThanOrEqualTo(buttonView.snp.top).offset(-40.dp)
}
Click here下载演示项目。
最佳答案
几件事...
首先,您要在“提示”标签上设置抗压性,这样它就不会被挤出来。
其次,当你给一个对象约束时,例如:
bottom of viewA greaterThanOrEqual to -40 from top of viewB
bottom of viewA lessThanOrEqual to -10 from top of viewB
你还没有给出完整的布局。
任何 -40 和 -10 之间的值是有效的,因此自动布局无法理解您真正想要的内容。
at least
-10 从 View B 的顶部 equalTo
-40 从 View B 的顶部 - 除非空间不够 - 所以给它一个Priority
小于 required
// MARK: DonMag ...
我进行更改的评论:
class MyView: UIView {
override class var requiresConstraintBasedLayout: Bool { return true }
// MARK: DonMag - a few varied length contentLabel strings
private var idx: Int = 0
private let examples: [String] = [
"Check your email.",
"Please check your email to activate your account.\n\nEnter the activation code:",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
]
private let headerView = UIButton().then {
$0.backgroundColor = .purple
$0.setTitle("Tap me!", for: .normal)
}
private let contentLabel = UILabel().then {
$0.font = .systemFont(ofSize: 21)
$0.numberOfLines = 0
$0.setContentHuggingPriority(.required, for: .vertical)
// MARK: DonMag - so we can see its frame
$0.textAlignment = .center
$0.backgroundColor = .cyan
}
private let codeTextField = UITextField().then {
$0.placeholder = "Enter code"
$0.backgroundColor = UIColor.white.withAlphaComponent(0.2)
$0.layer.cornerRadius = 8
$0.layer.borderWidth = 1
$0.layer.borderColor = UIColor.lightGray.cgColor
$0.textAlignment = .center
$0.font = .systemFont(ofSize: 21)
$0.keyboardType = .numberPad
}
private let tipsLabel = UILabel().then {
$0.font = .systemFont(ofSize: 12)
$0.adjustsFontSizeToFitWidth = true
$0.minimumScaleFactor = 0.2
$0.textAlignment = .center
$0.text = "If you have not received the code, tap 'resend'."
$0.setContentHuggingPriority(.required, for: .vertical)
// MARK: DonMag - prevent label from being compressed vertically
$0.setContentCompressionResistancePriority(.required, for: .vertical)
// MARK: DonMag - so we can see its frame
$0.backgroundColor = .yellow
}
private let bottomView = UIView().then {
$0.backgroundColor = .red
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
addSubview(headerView)
addSubview(contentLabel)
addSubview(codeTextField)
addSubview(tipsLabel)
addSubview(bottomView)
headerView.addTarget(self, action: #selector(headerViewDidTapped), for: .touchUpInside)
headerViewDidTapped()
}
override func updateConstraints() {
headerView.snp.updateConstraints { (make) in
make.leading.trailing.top.equalToSuperview()
make.height.equalToSuperview().multipliedBy(0.454)
}
bottomView.snp.updateConstraints { (make) in
make.leading.bottom.trailing.equalToSuperview()
make.height.equalTo(121)
}
contentLabel.snp.updateConstraints { (make) in
make.top.greaterThanOrEqualTo(headerView.snp.bottom).offset(10)
make.leading.equalTo(50)
make.trailing.equalTo(-50)
}
codeTextField.snp.updateConstraints { (make) in
make.top.equalTo(contentLabel.snp.bottom).offset(10)
make.leading.equalTo(46.5)
make.trailing.equalTo(-46.5)
make.height.equalTo(50)
}
tipsLabel.snp.updateConstraints { (make) in
make.top.equalTo(codeTextField.snp.bottom).offset(10)
make.width.centerX.equalTo(codeTextField)
make.bottom.greaterThanOrEqualTo(bottomView.snp.top).offset(-40)
// MARK: DonMag - tipsLabel bottom should be equalTo -40 with medium priority (so it can be overridden by auto-layout when needed)
//make.bottom.greaterThanOrEqualTo(bottomView.snp.top).offset(-40)
make.bottom.equalTo(bottomView.snp.top).offset(-40).priority(.medium)
make.bottom.lessThanOrEqualTo(bottomView.snp.top).offset(-10)
}
super.updateConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc
private func headerViewDidTapped() {
// MARK: DonMag - get example string for contentLabel
let text = examples[idx % examples.count]
idx += 1
contentLabel.text = text
setNeedsUpdateConstraints()
UIView.animate(withDuration: 0.1) {
self.layoutIfNeeded()
}
}
}
关于iOS Auto Layout 在空间不够的时候保持最小的间隙,在空间足够的时候尽可能的占用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63769505/
我一整天都在努力完成这项工作,但是 但没有成功。我尝试设置按钮顶部约束(垂直间距)相对于tableView。但是当我从我的快速代码中隐藏表格 View 时仍然占用空间。 我想要实现的是,当我隐藏表格
我一整天都在努力完成这项工作,但是 但没有成功。我尝试设置按钮顶部约束(垂直间距)相对于tableView。但是当我从我的快速代码中隐藏表格 View 时仍然占用空间。 我想要实现的是,当我隐藏表格
我不太擅长编程,但我做了一些研究,发现了这一小串 javascript,可以让我们在点击每个 javascript 链接按钮时立即在页面上播放声音: 问题是,当我放入其中几行来定义不同的声音时,它们
我该怎么做,让 .tabs__item2 占据右边的剩余空间? 我希望无论屏幕大小如何,.tabs__item2 都会填充剩余空间,一直到它的大蓝色 div 的边缘。可能类似于 width:100
我试图找到一种现代解决方案,在页面顶部有一个固定的菜单栏( flexbox ),在下面有一个 flexbox 。我希望页面滚动时菜单保持静态,第二个内容 flexbox 的内容滚动 (fixBox)。
我正在用 ajax 进度条和其他东西做一些事情...... 基本上我有一个隐藏的 div,里面写着“正在加载”,在它下面有一个可见的 div,里面有我的内容。 加载时,它淡出内容 div,使隐藏的 d
我有五个 div,每个都共享一个 ad_tile 类。该类在 div 的左侧和右侧提供 3 像素的填充和 1 像素的边框,并为内容提供清晰的修复。如果这些 div 中的一个或多个为空,则这些 div
有这个固定的 header元素( block 元素),带有 absolute定位nav带有菜单的元素,和另一个固定元素作为标志: About Contac
我有一个 ViewHolder,标题位于顶部,该标题在特定情况下变得可见。在大多数其他情况下, header 设置为 GONE。问题是,当 header 设置为 GONE 时,它的高度仍会计算,而其他
请注意,我的问题不是关于如何用 . 填充剩余空间 背景:我正在尝试将 jQuery sortable 与一系列水平布局的 , 结合使用与类 "column" .column { display:i
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improv
我有一个带有模板切换器的 ListView ,并且我希望在特定项目上隐藏它,所以我使用了一个隐藏模板。我将 View (或 StackLayout)设置为 isVisible=false 和 Heig
TL;DR:部署面向 ECS 的 CodePipeline 后,如何更改内存和 CPU 要求(或指定 ECS 任务定义的任何细节)? 我已经创建了一个部署 ECS 容器的 CodePipeline。为
哪种工具最适合测量 HDFS 空间消耗? 当我总结“hdfs dfs -du/”的输出时,与“hdfs dfsadmin -report”(“DFS Used”行)相比,我总是消耗更少的空间。是否有
我试图理解使用 MD5 哈希作为 Cassandra key 在“内存/存储消耗”方面的含义: 我的内容(在 Java 中)的 MD5 哈希 = byte[] 长 16 个字节。 (16 字节来自维基
我是一名优秀的程序员,十分优秀!