using sdk 4.2, I am adding a small button to the tabBarController in my app in my didFinishLaunchingWithOptions method of the app delegate. The idea is that this when made visible shows at the top of any viewcontroller showing on the screen. The user can tab between views and this button is still visible.
NSLog(@"tabBarController bounds: %@",NSStringFromCGRect(self.tabBarController.view.bounds));
self.Btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
self.Btn.frame = CGRectMake(80, 20, 160, 20);
self.Btn.hidden = YES;
[self.Btn addTarget:self action:@selector(launch:) forControlEvents:UIControlEventTouchUpInside];
[self.Btn setBackgroundImage:[UIImage imageNamed:@"launch.png"] forState:UIControlStateNormal];
[self.tabBarController.view addSubview:self.Btn];
Bounds of the tabBarcontroller as printed out by NSLog {{0, 0}, {320, 480}}
I make the frame of the button CGRectMake(80, 20, 160, 20); So i offset it 20 down to allow for the status bar. This works most of the time, when i make the 开发者_JAVA技巧button visible it shows in the right place at the top of the visible viewcontroller. Sometimes however the button moves downwards as though the bounds of the tabBarcontroller changed to 0,0,320,460. So the button is now relative to the new bounds so is an extra 20 pixels down from the top of the screen.
Once its moved it always stays like this until i kill the app from the task switcher and restart. Anybody any idea what would cause this and how to solve. Thanks
When you add the button to the tab bar controller’s view, set its autoresizing mask to UIViewAutoresizingFlexibleTopMargin
. That will “anchor” it to the bottom of the view, preventing height changes from moving it down.
For some reasons the bounds of the view of the tab bar controller changes. I don't know exactly why this happened in your case, but it's an expected behavior in some cases, for example when the user is having a phone call and the screen shows the in-call status bar.
I would adjust the autoresizing option of your button, so even if the bounds of the tab bar controller view changes, your button remains still. Fix the bottom margin and the content height of your button, and have a flexible top margin.
You can do this in IB, or programmatically setting autoresizingMask property.
精彩评论