Reputation: 20126
I have a programatically created status/overlay view that pops up when the device is busy doing some tasks. I am using the [view addSubview: statusView] to add it to the current view.
When the device rotates the statusView is rotating with the device, so the text ends up sideways. what do I change to ensure the subview does not get rotated, but instead it is repositioned?
There is a similar SO question here that suggests there might be some method I need to implement to handle the resizing/repositioning but it does not go into detail.
So far I am initialising the uiviewcontroller off screen as follows, and just moving it onto screen when it is required:
- (id)initWithStatus:(NSString*)_status forWindow: (UIWindow*) window {
if (self = [super init]) {
initialFrame = window.frame;
visible = NO;
barHeight = 40;
// Create the status bar below the windows drawing area
height = window.bounds.size.height;
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, height, window.bounds.size.width, barHeight)] autorelease];
[self.view setBackgroundColor:[UIColor blackColor]];
[self.view setAlpha:.5];
// Add text text
status = [[UILabel alloc] initWithFrame:CGRectMake(7, 7, window.bounds.size.width-14, barHeight-14)];
status.font = [UIFont systemFontOfSize: 14];
status.text = _status;
status.textAlignment = UITextAlignmentCenter;
status.backgroundColor = [UIColor clearColor];
status.textColor = [UIColor whiteColor];
[self.view addSubview:status];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
return self;
}
When in portrait orientation, sliding it onto the screen is easy using the following method:
-(void)updateStatus:(NSString*)_status {
if(visible) {
status.text = _status;
return;
}
visible = YES;
status.text = _status;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGRect frame = self.view.frame;
frame.origin.y = height - barHeight;
self.view.frame = frame;
[UIView commitAnimations];
}
And so when a change in orientation is detected, this is called:
- (void)orientationChanged:(NSNotification *)notification {
// Could just resize the box based on the new orientation but the text inside the box
// runs the wrong direction instead of along the box.
}
Upvotes: 0
Views: 870
Reputation: 118651
If you're using UIViewController, you can take advantage of autorotation. If you have a view that's not part of a view controller, normally your autoresizing mask will make sure the view is still centered correctly. If you want to reposition it manually, you can do so by observing the UIDeviceOrientationDidChangeNotification.
Upvotes: 1