當設備忙於執行某些任務時,會彈出一個以編程方式創建的狀態/覆蓋視圖。我正在使用[view addSubview:statusView]將其添加到當前視圖。當iPad/iPhone旋轉時旋轉子視圖
當設備旋轉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.
}
的全部源代碼的狀態欄[可在我的網站(http://jacobrhoden.com/StatusBarViewController.m),所有人都能看到或使用。 – Jacob