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;

}

调整后,可以看到,键盘弹出后,导航栏并没有消失

至此,问题完美解决!

原文地址:https://www.cnblogs.com/kw-ios/p/4182263.html