【问题标题】:UIView Mask Is Causing View To Move Positions (AutoLayout)UIView 掩码导致视图移动位置(自动布局)
【发布时间】:2018-12-11 01:27:26
【问题描述】:

在我的应用程序中,我有一个 UILabel,我用它来屏蔽 UIView。我在整个应用程序中使用 AutoLayout,发现在设置标签的掩码时,它的位置突然改变了。

这是我添加标签时的代码;

// Label
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Hello!"
label.font = UIFont.systemFont(ofSize: 50.0)
label.textColor = UIColor.white
view.addSubview(label)

// Constraints
label.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true

这会产生 结果。但是,在添加掩码时,通过以下代码;

// Mask
let mask = UIView()
mask.translatesAutoresizingMaskIntoConstraints = false
mask.backgroundColor = UIColor.blue
mask.mask = label
view.addSubview(mask)

// Constraints
mask.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
mask.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
mask.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
mask.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true

我的标签最终会重新定位,我正在寻求让文本保持在完美的中心位置。

【问题讨论】:

    标签: swift uiview autolayout


    【解决方案1】:

    您不能在用作掩码的视图上使用自动布局。该视图位于正常视图层次结构之外。您不会通过调用 addSubview(_:) 将其添加到视图层次结构中,只需将其设置为另一个视图的 mask 属性即可将其添加为掩码。

    因此,您必须将标签的框架直接设置为标签的中心。每次蒙版视图的框架发生更改时(例如,如果用户旋转设备),您还必须再次设置它。因此,您必须在viewDidLayoutSubviews()中设置标签的框架@

    我试图通过将标签的中心设置为视图的中心来使其工作,但这不起作用。不知何故,标签没有显示出来。我可以通过将标签大小显式设置为其intrinsicContentSize 来使其工作。我猜这是因为标签被用作蒙版而不是视图层次结构的一部分。

    这是一个工作示例。我冒昧地将命名从 mask 更改为 maskedView 以避免与 mask 属性混淆;-)

    class ViewController: UIViewController {
    
        var label = UILabel()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = .green
    
            // Label
            label.text = "Hello!"
            label.font = UIFont.systemFont(ofSize: 50.0)
            label.textColor = UIColor.white
    
            // Mask
            let maskedView = UIView()
            maskedView.translatesAutoresizingMaskIntoConstraints = false
            maskedView.backgroundColor = UIColor.blue
            maskedView.mask = label
            view.addSubview(maskedView)
    
            // Constraints
            maskedView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
            maskedView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
            maskedView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
            maskedView.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true
        }
    
        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            let labelHeight = label.intrinsicContentSize.height
            let labelWidth = label.intrinsicContentSize.width
            label.frame = CGRect(
                x: view.center.x - labelWidth / 2,
                y: view.center.y - labelHeight / 2,
                width: labelWidth,
                height: labelHeight
            )
        }
    }
    

    您可以通过设置label.textAlignment = .center 来缩短viewDidLayoutSubviews() 中的代码

    那么这就够了:

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        label.frame = view.frame
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 2016-03-28
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多