iOS開發中UIView層次管理中sendSubviewToBack用法


在iOS開發過程中,視圖的層次是非常重要的,往往一不小心就會造成出乎意料的bug。通常體現在button不靈敏或拖拽失效等;

Xcode6出來后,有個功能叫View UI Hierarchy,即UI檢視器。它的位置在xcode中的

它非常的方便我們觀察UI的位置及視圖的層次。

在iOS中管理視圖層次的二個重要方法就是

//將view顯示在最前

- (void)bringSubviewToFront:(UIView *)view;

例如:[self.view bringSubviewToFront:self.firstView];

//將view顯示在最后

- (void)sendSubviewToBack:(UIView *)view;

例如:[self.view sendSubviewToBack:self.firstView];

在這里我將着重講下sendSubviewToBack一個很妙的用法。

常常在處理文本框輸入內容時,通常是注冊一個通知,然后將view向上頂,

就像這樣

    //注冊鍵盤通知

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotification

                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillHide:)

                                                 name:UIKeyboardWillHideNotification

                                               object:nil];

然后再將界面向上頂

-(void) keyboardWillShow:(NSNotification *)note{

    // get keyboard size and loctaion

CGRect keyboardBounds;

    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];

    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    // Need to translate the bounds to account for rotation.

    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

// get a rect for the textView frame

    if ([self.contextView isFirstResponder]) {

        CGRect containerFrame = self.creatView.frame;

        containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);

        containerFrame.origin.x = 21;

        containerFrame.size.width=278;

        // animations settings

        [UIView beginAnimations:nil context:NULL];

        [UIView setAnimationBeginsFromCurrentState:YES];

        [UIView setAnimationDuration:[duration doubleValue]];

        [UIView setAnimationCurve:[curve intValue]];

        // set views with new info

        self.creatView.frame = containerFrame;

        // commit animations

        [UIView commitAnimations];

    }

}

但這樣做的話會有個不好的地方,那就是可能會將自定義的導航欄頂出界面外了,就像這樣

這樣的話雖說鍵盤並沒有擋住文本框的效果實現了,但界面體驗不太好。那么該怎么辦呢。

其實我們分析原因可知,之所以導航欄向上頂了,因為其所屬的層在整個view中

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

    if ([textView isKindOfClass:[self.secondview.descriptionTextView class]]) {

        [self.view sendSubviewToBack:self.secondview];

        [UIView animateWithDuration:0.35f animations:^{

            [self.secondview setFrame:CGRectMake(self.secondview.frame.origin.x, self.secondview.frame.origin.y - 216.0f, self.secondview.frame.size.width, self.secondview.frame.size.height)];

        } completion:^(BOOL finished) {

        }];

    }

    return YES;

}

調整后,可以看到,鍵盤彈出后,導航欄並沒有消失

至此,問題完美解決!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM