我有一个以编程方式创建的状态/覆盖视图,当设备忙于执行某些任务时会弹出。我正在使用[view addSubview:statusView]将其添加到当前视图。
当设备旋转时,statusView随设备一起旋转,因此文本会向侧面结束。如何更改以确保子视图不会旋转,而是重新定位?
有一个similar SO question here表明我可能需要实现一些方法来处理调整大小/重新定位,但它没有详细说明。
到目前为止,我正在按照以下方式在屏幕上初始化uiviewcontroller,并在需要时将其移动到屏幕上:
- (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;
}
在纵向方向时,使用以下方法将其滑动到屏幕上很容易:
-(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];
}
因此,当检测到方向变化时,这称为:
- (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.
}
答案 0 :(得分:1)
如果您正在使用UIViewController,则可以利用autorotation。如果您的视图不属于视图控制器,通常您的自动调整遮罩将确保视图仍然正确居中。如果要手动重新定位,可以通过观察UIDeviceOrientationDidChangeNotification。
来实现