I have a UIView which I would like to have aligned just above the UITabBar in both portrait and landscape orientation. I would like to do this programmatically.
This is all I have in my UIView subclass, IPChatTextView
:
/* Initialization */
- (id)init
{
if (self = [super init])
{
// Set frame
self.frame = CGRectMake(0, 0, 320, 40);
// Create background
UIEdgeInsets insetsBackground = UIEdgeInsetsMake(20, 50, 19, 82);
UIImage *imageBackground = [[UIImage imageNamed:@"ChatTextView_Background.png"] resizableImageWithCapInsets:insetsBackground];
UIImageView *imageViewBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
imageViewBackground.autoresizingMask = UIViewAutoresizingFlexibleWidth;
imageViewBackground.image = imageBackground;
// Add subviews
[self addSubview:imageViewBackground];
}
return self;
}
The image view imageViewBackground
should fill the entire UIView and therefore I have set it's autoresizing mask to UIViewAutoresizingFlexibleWidth
. This works fine.
When initializing the view, I set it's autoresizing mask.
self.chatTextView = [[IPChatTextView alloc] init];
self.chatTextView.frame = CGRectMake(0, 327, 320, 40);
self.chatTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:self.chatTextView];
I have tried adding a UIView in Interface Builder and set it's autoresizing mask as in the image below and it works perfectly.
I thought that when I want to have the same autoresizing as the image above, I would do it programmatically as the code below but it doesn't give me the same result. Instead it is positioned about 2/3 from the top, which seems odd as I set the y-position of the self.chatTextView
to 327 which is just above the tab bar.
self.chatTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
Does anyone know how I should set my autoresizing mask when I want the view to be positioned above the tab bar in both portrait and landscape orientation?
EDIT:
If I change the autoresizing mask to include UIViewAutoresizingMaskFlexibleBottomMargin
instead of UIViewAutoresizingMaskFlexibleTopMargin
, the view is aligned above the tab bar in portrait mode but is gone out of the screen in landscape mode.
I ended up positioning it programmatically when a rotation was detected.