首页 > 解决方案 > 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."}
    }
}

标签: swiftautolayoutnslayoutconstraint

解决方案


您没有提供足够的信息来重现问题。这是您的代码的减少,在我的视图控制器中viewDidLoad(我消除了除标签和开关之外的所有内容,修复了您的leftand 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)])

结果显示正常;没有约束冲突或歧义,它看起来和预期的一样:

在此处输入图像描述


推荐阅读