2011-08-24 71 views
0

當設備忙於執行某些任務時,會彈出一個以編程方式創建的狀態/覆蓋視圖。我正在使用[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. 
} 
+0

的全部源代碼的狀態欄[可在我的網站(http://jacobrhoden.com/StatusBarViewController.m),所有人都能看到或使用。 – Jacob

回答

1

如果您使用UIViewController,則可以利用autorotation。如果您的視圖不是視圖控制器的一部分,通常您的自動調整屏蔽將確保視圖仍然正確居中。如果您想手動重新定位,可以通過觀察UIDeviceOrientationDidChangeNotification來完成。

+0

我觀察了'UIDeviceOrientationDidChangeNotification'我的問題圍繞不知道該怎麼辦時,調用此事件,即我可以當一個旋轉事件發生時左右移動我的子視圖/盒,但箱內文本橫盤或倒置結束。 – Jacob

+1

您還可以設置視圖的['transform'](http://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/transform)屬性來旋轉它等等。沒有更多的背景,我無法提供詳細的建議。 – jtbandes

+0

我已經添加了初始化uiviewcontroller的代碼。當設備未旋轉時,此代碼正常工作。當它旋轉時,代碼仍然可以工作,但是它顯示了錯誤位置的橫條,並且文本的方向錯誤。 – Jacob