如何以编程方式根据纵向/横向更改布局约束?

时间:2018-09-24 16:22:57

标签: ios swift autolayout

我以编程方式构建所有对象和标签,并具有一个名为setupLayout()的函数,如下所示:

 private func setupLayout() {

//The Image that presents the Logo of the chosen cryptocurrency.
    view.addSubview(cryptoIconImage)
    cryptoIconImage.translatesAutoresizingMaskIntoConstraints = false
    cryptoIconImage.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    cryptoIconImage.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
    cryptoIconImage.widthAnchor.constraint(equalToConstant: 60).isActive = true
    cryptoIconImage.heightAnchor.constraint(equalToConstant: 60).isActive = true


//The Label that presents the name of the chosen cryptocurrency.
    view.addSubview(cryptoLabel)
    cryptoLabel.translatesAutoresizingMaskIntoConstraints = false
    cryptoLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    cryptoLabel.topAnchor.constraint(equalTo: cryptoIconImage.bottomAnchor, constant: 10).isActive = true
    cryptoLabel.font = customFont?.withSize(32)
}

这可以使布局保持美观和居中,并且适合人像使用,也可以在横向使用以实现相同效果。问题是,在横向环境中,我不再希望元素居中,我想将某些元素移至屏幕左侧,而另一些元素移至右侧(我没有显示安装程序中的所有项目)其他答案这个问题似乎都使用情节提要,我发现这种方法可以使用:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    let orient = UIApplication.shared.statusBarOrientation
    print(orient)

    switch orient {
    case .portrait:
        print("Portrait")
        break
    // Do something
    default:
        print("LandScape")
        // Do something else
        self.landscapeLayout()
        break
    }
}

但是我不确定应该如何更改删除和添加新的约束,以便在来回移动时平稳运行。我考虑过要使用如下的景观布局函数():

    private func landscapeLayout() {
    view.addSubview(cryptoIconImage)
    cryptoIconImage.translatesAutoresizingMaskIntoConstraints = false
//Removing centerXAnchor
    cryptoIconImage.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = false
    cryptoIconImage.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
    cryptoIconImage.widthAnchor.constraint(equalToConstant: 60).isActive = true
    cryptoIconImage.heightAnchor.constraint(equalToConstant: 60).isActive = true
//Putting it to the left side
    cryptoIconImage.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30).isActive = true

}

但是这给我带来了拉伸等问题,并且在添加标签新内容时摆脱了标签内容。我应该如何在纵向和横向之间设置两种不同的布局?

1 个答案:

答案 0 :(得分:0)

那么您可以执行以下操作:

声明变量以保留约束。

var cryptoIconImageCenterXWhenPortrait : NSLayoutConstraint?
var cryptoIconImageCenterXWhenLandscape : NSLayoutConstraint?

定义两个约束

cryptoIconImageCenterXWhenPortrait = cryptoIconImage.centerX.constraint(equalTo: window.centerX)
cryptoIconImageCenterXWhenLandscape = cryptoIconImage.centerX.constraint(equalTo: window.centerX, constant: -20)

相应地激活它们

If Landscape {
   cryptoIconImageCenterXWhenPortrait.isActive = false
   cryptoIconImageCenterXWhenLandscape.isActive = true
} else If Portrait {
   cryptoIconImageCenterXWhenPortrait.isActive = true
   cryptoIconImageCenterXWhenLandscape.isActive = false
}