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).
精彩评论