swift - iOS 应用程序中的布局问题 (NSLayoutConstraint)
问题描述
在 iOS 应用程序中,我遇到了自动布局问题。
以下 2 个屏幕截图显示了该问题。
右侧的开关(UISwitch 对象)在应该固定时水平移位。谁能看到发生了什么?
左边的字符串确实在改变长度,但我认为(根据我设置约束的方式)应该调整字体大小或将字符串分成两行;但不是开关移位。
这是相关的快速代码:
import UIKit
class My_ViewController: UIViewController {
let xPanel = UILabel(), yPanel = UILabel(),
khToggle = UISwitch(), khLabel = UILabel()
....
override func viewDidLoad() {
super.viewDidLoad()
layOutUI()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
.....
toggleKeepHide(khToggle)
}
func layOutUI() {
for component in [xPanel,yPanel,khLabel,khToggle] {
component.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(component)
}
...........
khLabel.numberOfLines = 0
khLabel.adjustsFontSizeToFitWidth = true
khToggle.addTarget(self,
action: #selector(toggleKeepHide(_:)),
for: .valueChanged)
view.addConstraints([
.........
NSLayoutConstraint(item: khToggle, attribute: .right, relatedBy: .equal,
toItem: view, attribute: .right, multiplier: 1.0, constant: -30.0),
NSLayoutConstraint(item: khToggle, attribute: .top, relatedBy: .equal,
toItem: yPanel, attribute: .bottom, multiplier: 1.0,
constant: 50.0),
NSLayoutConstraint(item: khLabel, attribute: .right, relatedBy: .equal,
toItem: khToggle, attribute: .left, multiplier: 1.0, constant: -23.0),
NSLayoutConstraint(item: khLabel, attribute: .centerY, relatedBy: .equal,
toItem: khToggle, attribute: .centerY, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: khLabel, attribute: .left, relatedBy: .equal,
toItem: view, attribute: .left, multiplier: 1.0, constant: 30.0)])
}
@objc func toggleKeepHide(_ sender: UISwitch) {
if sender.isOn {khLabel.text = "Hide this object from the wyxoug list."}
else {khLabel.text = "Keep this object in the wyxoug list."}
}
}
解决方案
您没有提供足够的信息来重现问题。这是您的代码的减少,在我的视图控制器中viewDidLoad
(我消除了除标签和开关之外的所有内容,修复了您的left
and right
(您永远不应该使用它们),并将两个视图之间的对齐方式更改为top
而不是center
):
khToggle = UISwitch()
khToggle.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(khToggle)
khLabel = UILabel()
khLabel.text = String(repeating: "word ", count: 40)
khLabel.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(khLabel)
// Do any additional setup after loading the view.
khLabel.numberOfLines = 0
view.addConstraints([
NSLayoutConstraint(item: khToggle!, attribute: .trailing, relatedBy: .equal,
toItem: view, attribute: .trailing, multiplier: 1.0, constant: -30.0),
NSLayoutConstraint(item: khToggle!, attribute: .top, relatedBy: .equal,
toItem: view, attribute: .top, multiplier: 1.0,
constant: 50.0),
NSLayoutConstraint(item: khLabel!, attribute: .trailing, relatedBy: .equal,
toItem: khToggle, attribute: .leading, multiplier: 1.0, constant: -23.0),
NSLayoutConstraint(item: khLabel!, attribute: .top, relatedBy: .equal,
toItem: khToggle, attribute: .top, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: khLabel!, attribute: .leading, relatedBy: .equal,
toItem: view, attribute: .leading, multiplier: 1.0, constant: 30.0)])
结果显示正常;没有约束冲突或歧义,它看起来和预期的一样:
推荐阅读
- python - 从两个数据帧按数量循环
- javascript - 如何在 react.js 中拥有每 5 秒更改一次的图像?
- java - 无法打开java动态web项目
- imagick - 使用转换时,Pngs 为空白
- node.js - 在 Node.js 中从 PostgreSQL 读取数据
- css - 如何在 React Bootstrap 中使 div 可单独滚动
- node.js - 当我使用 Pdfkit 创建第二个 PDF 时,此文件已损坏。ExpressJS
- node.js - 以具有 root 权限的用户身份启动 PM2 进程
- debugging - Android Studio IDE - 调试步骤进入/过度崩溃应用程序
- php - Html/Php 输入类型字段标签/列表