ios11 劉海屏 安全區域 適配 彈框區域適配

weixin_34402408發表於2018-11-06

網上看了很多 自己的程式碼裡面最終還是採用了自己的辦法

我採用安全區域做

首先在基類自定義了個vkview 如果非SB或者xib做的話 就可以用到

- (UIView*)vkView {

    if(!_vkView) {

        if(@available(iOS11.0, *)) {

            self.view.backgroundColor=VKHexColor(app_White_Color);

            _vkView= [[UIViewalloc]init];

            [self.viewaddSubview:_vkView];

            [_vkViewmas_makeConstraints:^(MASConstraintMaker*make) {

                make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop);

                make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);

                make.left.mas_equalTo(self.view.mas_safeAreaLayoutGuideLeft);

                make.right.mas_equalTo(self.view.mas_safeAreaLayoutGuideRight);

            }];

        }else{

            //適配狀態列

            if (self.navigationController.navigationBarHidden || !self.navigationController) {

                self.view.backgroundColor=VKHexColor(app_White_Color);

                _vkView= [[UIViewalloc]init];

                [self.viewaddSubview:_vkView];

                [_vkViewmas_makeConstraints:^(MASConstraintMaker*make) {

                    make.top.mas_equalTo(self.view).offset(20);

                    make.bottom.left.right.mas_equalTo(self.view);

                }];

            }else{

                _vkView=self.view;

            }

        }

        _vkView.backgroundColor = VKHexColor(app_White_Color);

    }

    return _vkView;

}

用vkview的時候 記得在點語法之前 告知導航欄隱藏的情況

二 ,劉海屏有時候需要判斷

1》window.safeAreaLayoutGuide.layoutFrame.size.height  !=kScreenHeight   kScreenHeight是巨集屏高

2》[UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom > 0

三,有時候需要彈框 需要精確定位

self是封裝的彈框view

[[[UIApplication sharedApplication].keyWindow.subviews objectAtIndex:0] addSubview:self];

    if(@available(iOS11.0, *)) {

        [selfmas_makeConstraints:^(MASConstraintMaker*make) {

            make.top.mas_equalTo(_toPoint.y);

            make.left.mas_equalTo([[UIApplication sharedApplication].keyWindow.subviews objectAtIndex:0].mas_safeAreaLayoutGuideLeft);

            make.right.mas_equalTo([[UIApplication sharedApplication].keyWindow.subviews objectAtIndex:0].mas_safeAreaLayoutGuideRight);

            make.bottom.mas_equalTo([[UIApplication sharedApplication].keyWindow.subviews objectAtIndex:0].mas_safeAreaLayoutGuideBottom);

        }];

    }else{

        [selfmas_makeConstraints:^(MASConstraintMaker*make) {

            make.top.mas_equalTo(_toPoint.y);

            make.left.mas_equalTo([[UIApplication sharedApplication].keyWindow.subviews objectAtIndex:0].mas_left);

            make.right.mas_equalTo([[UIApplication sharedApplication].keyWindow.subviews objectAtIndex:0].mas_right);

            make.bottom.mas_equalTo([[UIApplication sharedApplication].keyWindow.subviews objectAtIndex:0].mas_bottom);

        }];

    }

其中topoint是

//獲取絕對高度

            UIWindow*window = [[[UIApplicationsharedApplication]delegate]window];

            CGRectrect = [weakSelf.segmentViewconvertRect:weakSelf.segmentView.boundstoView:window];

CGPointMake(0, rect.origin.y+rect.size.height) 獲取

相關文章