【问题标题】:Masking a UIView and Auto Layout屏蔽 UIView 和自动布局
【发布时间】:2015-03-26 20:00:18
【问题描述】:

我有一个 UIView,我想用另一个 UIView 掩盖它,从它的中心打一个洞。这是我的viewDidLoad

- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.viewToMask];
[self.view addSubview:self.theMask];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewToMask attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewToMask attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[cyan(200)]" options:0 metrics:nil views:@{@"cyan": self.viewToMask}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[cyan(200)]" options:0 metrics:nil views:@{@"cyan": self.viewToMask}]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.theMask attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.viewToMask attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.theMask attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.viewToMask attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[mask(100)]" options:0 metrics:nil views:@{@"mask": self.theMask}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[mask(100)]" options:0 metrics:nil views:@{@"mask": self.theMask}]];
}

它准确地给出了我正在寻找的东西,减去掩蔽:

如果我再添加一行:

[self.viewToMask setMaskView:self.theMask];

两个视图都消失了 --- 小视图 (self.theMask) 掩盖了整个大视图 (self.viewToMask),即使它只有一半大小。有谁明白这里发生了什么?自动布局不能用UIView.maskView吗?

【问题讨论】:

  • 我知道当你使用iOS 8之前的版本(-[CALayer maskLayer])时,你必须定位遮罩层的坐标空间,而遮罩层在普通视图层次结构中不存在.我想知道这里是否发生了类似的事情?
  • 这完全是这里发生的事情。非常感谢。
  • 那么效果如何?你能用maskView 使用自动布局吗?

标签: ios objective-c uiview autolayout


【解决方案1】:

正如 Zev 所解释的,遮罩视图位于普通视图层次结构之外,因此不能与自动布局一起使用。我通过手动将其放置在我的视图控制器的viewDidLayoutSubviews 中解决了这个问题:

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    CGRect viewToMaskRect = self.viewToMask.bounds;
    CGRect maskRect = CGRectMake(viewToMaskRect.origin.x + 50.0, viewToMaskRect.origin.y + 50.0, 100.0, 100.0);
    [self.theMask setFrame:maskRect];
    [self.viewToMask setMaskView:self.theMask];
}

【讨论】:

【解决方案2】:

您可以通过将 maskView 设置为普通子视图(例如,在具有相关约束的情节提要中)然后仅将其设置为 viewWillLayoutSubviews 中的 maskView 来使您的 maskView 自动布局。即:

-(void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    self.viewToBeMasked.maskView = self.maskViewWithLayoutConstraints;
}

【讨论】:

  • 如果不嵌套遮罩视图,这对我不起作用。这有点奇怪。 :/我将其作为替代答案发布。
【解决方案3】:

根据Russ' 的回答,我进行了自己的研究并找到了替代解决方法。

下面的代码示例将用 10pt 遮盖主视图的左侧和右侧。重要的

class SomeViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.whiteColor()

        self.setupMaskView()
    }

    private func setupMaskView() {

        let maskView = UIView()
        maskView.translatesAutoresizingMaskIntoConstraints = false
        maskView.backgroundColor = UIColor(white: 0.0, alpha: 1.0)

        let maskContainerView = UIView()
        maskContainerView.addSubview(maskView)

        self.view.addSubview(maskContainerView)

        maskView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor, constant: 10).active = true
        maskView.widthAnchor.constraintEqualToAnchor(self.view.widthAnchor, constant: -20).active = true
        maskView.heightAnchor.constraintEqualToAnchor(self.view.heightAnchor).active = true

        self.view.maskView = maskContainerView // this will not work it we place this line above the constraint code !!!
        /* I have no idea why this does not work and why nesting is required, maybe some sort of bug? */
    }
}

此代码使用 iOS 9.0 和 Swift 2.0 提供的 AutoLayout 语法。 (用 Xcode 7 beta 4 编写)

【讨论】:

  • 这其实是一个很聪明的把戏!至于代码中的注释 - 当视图不在层次结构中时,您无法设置约束。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
相关资源
最近更新 更多