2012-05-08 68 views
0

我已經在我的應用程序如下界面佈局的觀點:iOS版 - 接口單元重疊的時候把

enter image description here

當我在文本字段上單擊(如果你舔添加標籤添加一個新文本字段)我把我的觀點向上滾動,以不通過鍵盤進行遮擋,使用戶可以正確地看到他正在打字,但是當我這樣做會發生以下情況:

enter image description hereenter image description here

這是我使用的代碼進行切換:

- (void)keyboardWillShow:(NSNotification *)notification { 
    if (self.keyboardIsShown) { 
    return; 
    } 

    NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey]; 
    CGSize keyboardSize = [keyboardBoundsValue CGRectValue].size; 

    NSTimeInterval animationDuration = 0.3; 

    CGRect frame = self.view.frame; 

    frame.origin.y -= keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y; 
    frame.size.height += keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y; 

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:animationDuration]; 
    self.view.frame = frame; 
    [UIView commitAnimations]; 

    self.keyboardIsShown = YES; 
} 

- (void)keyboardWillHide:(NSNotification *)notification { 
    NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey]; 
    CGSize keyboardSize = [keyboardBoundsValue CGRectValue].size; 

    NSTimeInterval animationDuration = 0.3; 

    CGRect frame = self.view.frame; 

    frame.origin.y += keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y; 
    frame.size.height -= keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y; 

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:animationDuration]; 
    self.view.frame = frame; 
    [UIView commitAnimations]; 

    self.keyboardIsShown = NO; 
} 

任何想法,我怎樣才能使視圖元素顯示導航欄或消失或其他變通方法,將正常工作的背後?

回答

1

一個快速實現你想要的方法是讓你的導航欄在視圖樹中的「內容」視圖下面。這樣導航欄將保持在最前面。如果您動態添加內容。確保將它們添加爲導航欄後面的視圖的子視圖。

//編輯

正如意見建議:

self.view.clipsToBounds = YES; 
CGRect frame = self.yourWrappingView.frame; 
NSUInteger distanceToMove = 200; 
self.yourWrappingView.frame = CGRectMake(frame.origin.x - distanceToMove, frame.origin.y, frame.size.width, frame.size.height); 

這應該爲你工作交配:)

+0

它都是在故事板中創建的,但add標籤中的附加表格視圖單元格除外。 – 8vius

+0

因此,將您在視圖控制器主視圖內的視圖中移動的內容封裝起來。問題是,你正在移動視圖控制器的主視圖,你不應該這樣做。 –

+0

好的,我應該怎麼做?有像你這樣的子視圖,把所有東西都包起來了嗎? – 8vius

0

也許你沒有使用真正的UINavigationController。但你應該。所以你不會有這些問題和正確的調整行爲。但要解決它在當前的體系結構:

  • 確保導航欄在你的筆尖文件最頂端的對象/分鏡
  • 添加一個IBOutlet您UINavigationBar的,如果你動態插入新的觀點,使用[self insertSubview: newView belowSubview: navigationBarOutlet];
+0

我的視圖嵌入在導航控制器中,我使用的是iOS 5.1。並且只有表格視圖的單元格被動態插入。 – 8vius