更改UIView的变换不会影响约束 [英] Changing UIView's transform doesn't affect constraints

查看:89
本文介绍了更改UIView的变换不会影响约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 11中,更改UIView的transform属性(例如scale)不会影响绑定到该视图的约束.在iOS 10中,一切正常.

In iOS 11 changing UIView's transform property (e.g. scale) doesn't affect constraints that bound to this view. In iOS 10 all works as expected.

我拥有的代码:

import UIKit

class TestViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        // top view
        let topView = UIView()
        topView.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
        topView.backgroundColor = .black
        view.addSubview(topView)

        topView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)

        // bottom view
        let bottomView = UIView()
        bottomView.translatesAutoresizingMaskIntoConstraints = false
        bottomView.backgroundColor = .black
        view.addSubview(bottomView)

        NSLayoutConstraint.activate([
            bottomView.heightAnchor.constraint(equalToConstant: 200),
            bottomView.widthAnchor.constraint(equalToConstant: 200),
            bottomView.leftAnchor.constraint(equalTo: topView.leftAnchor),
            bottomView.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: 8)
        ])
    }
}

期望(iOS 10)

拥有什么(iOS 11):

如您所见,topView已缩放,但bottomView布局不正确.我该如何解决?

As you can see, topView scaled, but bottomView doesn't laid out correctly. How can I fix it?

推荐答案

来自Apple的UIView / Transform文档( https://developer.apple.com/documentation/uikit/uiview/1622459-transform ):

From Apple's UIView / Transform Docs (https://developer.apple.com/documentation/uikit/uiview/1622459-transform):

警告

当此属性的值不是恒等变换时,则frame属性中的值是未定义的,并且 应该被忽略.

When the value of this property is anything other than the identity transform, the value in the frame property is undefined and should be ignored.

因此,您在iOS 10中获得的结果恰好是您获得的结果,而不是正确的结果.

So, the result you got in iOS 10 just happened to be what you got, rather than it being the correct result.

这篇关于更改UIView的变换不会影响约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆