1

I am trying to set mask on my blue UIView for which I have to set frame for mask but problem is blue UIView is using auto layout.

How can I get x, y, width, height of UIView which is using auto layout to use like this [self.v.layer.mask setFrame:CGRectMake(?, ?, ?, ?)];.

Is using frame property like self.v.frame.size.width; is correct while using auto layout? or is there any other approach of getting an UIView x, y, width, height which is using auto layout.

Please see the screen shot below.

enter image description here

S.J
  • 3,063
  • 3
  • 33
  • 66
  • are you not able to achieve what you are looking for using `self.v.frame.size.width` ? – iphondroid Aug 26 '15 at 10:31
  • 1
    @iphondroid what I read is not use frame when using auto layouts. I have no idea that I am getting correct or wrong values. – S.J Aug 26 '15 at 10:33

2 Answers2

0

You won't get/set the correct frame when using autolayout system. If you really want to do this. You can use below method

- (void)viewDidLayoutSubviews {
}

it is recommended that you should use constraints to update the size/position of your view by updating the constraints.

eg: make IBOutlet of you view's constraint and view and then update like this

self.myViewWidthConstraint.constant += 20;
[self.myView updateConstraints];

To get the width you should use

self.myViewWidthConstraint.constant;
Rohit Kumar
  • 877
  • 6
  • 20
-1

When people talk about not using frames with autolayout they mean 'don't set frames that are handled by autolayout', because autolayout will handle setting the frame correctly. Reading the frame's value is not a problem.

But be aware that the frame's value may be changed by autolayout whenever the view is laid out. So you may need to put your update code in viewDidLayoutSubviews, e.g.:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.blueView.layer.mask.frame = self.blueView.bounds;
}

Also be aware that if your mask layer is drawn based on its frame, it will need to redraw its content whenever its frame changes.

johnpatrickmorgan
  • 2,372
  • 2
  • 13
  • 17