繁体   English   中英

Swift 4:设备旋转后,UI无法更新

[英]Swift 4: UI fails to update after device rotation

当我从纵向->横向移动时,我想将我的bearImageView的宽度和高度更改为100,而向相反方向时,更改为200。

setupLayout内部,我调用停用,然后激活widthAnchorheightAnchor的约束。 因此,我期望它会改变宽度和高度。

问题:变为100,但没有变回200。为什么会发生这种情况?

这是代码。

class ViewController: UIViewController {

    // closure objects
    let bearImageView: UIImageView = {
        let imageView = UIImageView(image: #imageLiteral(resourceName: "bear_first")) // type `image literal` and double click
        imageView.translatesAutoresizingMaskIntoConstraints = false // enable autolayout
        return imageView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()            
        view.addSubview(bearImageView) // display image        
        setupLayout(imageView: bearImageView) // apply constraints
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        if UIDevice.current.orientation.isLandscape {
            print("landscape")
            setupLayout(imageView: bearImageView) // apply constraints                
        } else {
            print("portrait")
            setupLayout(imageView: bearImageView) // apply constraints                
        }
    }

    private func setupLayout(imageView: UIImageView){

        if UIDevice.current.orientation.isLandscape == true {
            imageView.widthAnchor.constraint(equalToConstant: 200).isActive = false
            imageView.heightAnchor.constraint(equalToConstant: 200).isActive = false
            imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
            imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
            imageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
            print("changed to landscape")

        } else {
            imageView.widthAnchor.constraint(equalToConstant: 100).isActive = false
            imageView.heightAnchor.constraint(equalToConstant: 100).isActive = false
            imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
            imageView.widthAnchor.constraint(equalToConstant: 200).isActive = true
            imageView.heightAnchor.constraint(equalToConstant: 200).isActive = true
            print("changed to portrait")
        }

    }

这是错误。

[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. 
(
    "<NSLayoutConstraint:0x6000031923a0 UIImageView:0x7fbf72508060.width == 200   (active)>",
    "<NSLayoutConstraint:0x6000031e4730 UIImageView:0x7fbf72508060.width == 100   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000031923a0 UIImageView:0x7fbf72508060.width == 200   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

请注意, constraint方法为您的图像视图创建了new

首次在viewDidLoad调用setupLayout时,宽度和高度限制为200会添加到图像视图中。 然后旋转设备以更改为横向。 setupLayout被再次调用。 这次,它添加了100的宽度和高度约束, 但是并没有停用先前添加的常数200的约束 做这行:

imageView.widthAnchor.constraint(equalToConstant: 200).isActive = false

创建新的非活动约束, 而不停用旧的约束。

您应该做的是将宽度和高度约束存储为ViewController属性:

var widthConstraint: NSLayoutConstraint!
var heightConstraint: NSLayoutConstraint!

然后在setupLayout ,分配给那些属性:

private func setupLayout(imageView: UIImageView){
    widthConstraint = imageView.widthAnchor.constraint(equalToConstant: 200)
    heightConstraint = imageView.heightAnchor.constraint(equalToConstant: 200)
    widthConstraint.isActive = true
    heightConstraint.isActive = true
    imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true

}

而且您应该只调用一次setupLayout

然后,创建另一个名为updateConstraints方法,该方法更新widthConstraintheightConstraint的常量:

private func updateConstraints() {
    if UIDevice.current.orientation.isLandscape {
        heightConstraint.constant = 100
        widthConstraint.constant = 100
    } else {
        heightConstraint.constant = 200
        widthConstraint.constant = 200
    }
}

viewWillTransitionToSize而不是setupLayout调用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM