开发者

Override UIView's frame

开发者 https://www.devze.com 2023-02-27 14:14 出处:网络
I have subclassed UIView, but I need to prevent the frame from changing, so I tried overriding the setFrame: method and just ignoring the value passed, and creat开发者_JS百科ing my own CGRect base on

I have subclassed UIView, but I need to prevent the frame from changing, so I tried overriding the setFrame: method and just ignoring the value passed, and creat开发者_JS百科ing my own CGRect base on self.superview and passing it to [super setFrame:

How can I make the UIView's frame unchangeable? (from within the subclass)


Step1: Override setFrame to do nothing.

- (void)setFrame:(CGRect)newRect
{
}

Step 2: Move your semi-static frame setting into your overridden version of willMoveToSuperview: to get a valid superview reference.

- (void)willMoveToSuperview:(UIView *)newSuperview
{
   CGRect newFrame = newSuperview.frame;
   //manipulate the frame here
   [super setFrame:newFrame];
}


Wait, can't you just set the autoresizingMask to 0? I though that would prevent the view from getting resized when parent view is resized (tried it as well, and seemed to work for me).

0

精彩评论

暂无评论...
验证码 换一张
取 消