美文网首页
ios系统相册调用遇到的导航坑

ios系统相册调用遇到的导航坑

作者: 码农_会写诗 | 来源:发表于2020-03-19 15:44 被阅读0次

最近几天在搭建项目的新框架,在调用系统相册的时候(Xcode11)居然遇到了一些UIImagePickerController导航栏的坑,在这里记录下。

在这里我就不多说调用相册和相机的操作了(基操作,不赘述)。

1.我在弹出UIImagePickerController的时候如下

在调用presentViewController方法时会发现顶部上面有距离,原因是:苹果将UIViewController的modalPresentationStyle属性的默认值改成了新加的一个枚举值UIModalPresentationAutomatic,对于多数UIViewController,此值会映射成UIModalPresentationPageSheet。

解决办法: 可以在vcpresent之前设置modalPresentationStyle 为 UIModalPresentationFullScreen(imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;)

2.打开相册后会发现整体往上偏移,

这里是我在我的baseVC里面设置了

if (@available(iOS 11.0, *)) {

        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];

}来防止scrollerView在滑动的时候会在顶部留一个小的空隙。既然找到这个问题 ,那我们只需要在相册弹出时修改下即可

if (@available(iOS 11, *)) {

        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;

}

然后在回到我们的app时重新写下

if (@available(iOS 11.0, *)) {

        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];

}

ok,问题解决。

3.在present出来相册控制器上,导航是透明的,用户体验很不友好。这里我们需要设置下

#pragma mark--解决UIImagePickerController导航透明问题
- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    if ([navigationController isKindOfClass:[UIImagePickerController class]])
    {  
        navigationController.navigationBar.translucent = NO;
        viewController.navigationController.navigationBar.translucent = NO;
        viewController.edgesForExtendedLayout = UIRectEdgeNone;
        //设置成想要的背景颜色
//       [navigationController.navigationBar setBarTintColor:RedBackColor];
    
    }
}

很奇怪的一点是 我这样设置了在ios13之前的版本都是没有问题的,我跑了一下ios13发现雪崩,毛用没有。这可以肯定是ios13导航改变导致的。然后我就在上面加了一句

if (@available(iOS 13, *)) {

//          [[UINavigationBar appearance]setTranslucent:NO];

        }

好像是解决了这个bug,但是会有一点瑕疵。

4.修改UIImagePickerController导航右侧取消按钮

默认弹出是英文的,本来以为需要自定义或者重写下,后面发现在info里配置个chinese就可以了。

至此,我这次用个相册遇到的问题及解决方法基本都在这了。但是我觉得代码还是不友好,欢迎遇到类似问题的道友指正。谢谢!

相关文章

网友评论

      本文标题:ios系统相册调用遇到的导航坑

      本文链接:https://www.haomeiwen.com/subject/pkdlyhtx.html