开发者

Override UIView's frame

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜