UIViewController相关
发布日期:2022-03-18 08:27:47 浏览次数:29 分类:技术文章

本文共 4691 字,大约阅读时间需要 15 分钟。

UIViewController相关


iOS7中引入automaticallyAdjustsScrollViewInsets,让系统根据是否存在顶部或底部导航元素(UINavigationBar类型)自动计算scrollview的inset,该属性默认设置为true

automaticallyAdjustsScrollViewInsets

automaticallyAdjustsScrollViewInsets现已废弃,被UIScrollViewcontentInsetAdjustmentBehavior所代替,可参考:

never


UIViewController自定义导航栏,可参考:


在iOS11中UIViewController的topLayoutGuidebottonLayoutGuide的两个属性被废弃,被UIView的safe area替代了

safe area

参考:

如下的UIViewController的扩展:

extension UIViewController {    var sa_safeAreaInsets: UIEdgeInsets {        if #available(iOS 11, *) {
return view.safeAreaInsets } return UIEdgeInsets(top: topLayoutGuide.length, left: 0.0, bottom: bottomLayoutGuide.length, right: 0.0) } var sa_safeAreaFrame: CGRect { if #available(iOS 11, *) {
return view.safeAreaLayoutGuide.layoutFrame } return UIEdgeInsetsInsetRect(view.bounds, sa_safeAreaInsets) }}

在iOS 11中UIViewController有一个新的属性:

@available(iOS 11.0, *)open var additionalSafeAreaInsets: UIEdgeInsets

,使用此属性调整view controller’s view的safe area insets。

如果在控制器的底部有个自定义的view,可使用additionalSafeAreaInsets

self.additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: self.ibToolbar.bounds.height, right: 0)

在Safe Area的inset改变时,得到通知,可使用UIViewsafeAreaInsetsDidChange方法,或者UIViewControllerviewSafeAreaInsetsDidChange


layoutMargins属性

可参考:

在iOS8中引入layoutMargins属性,使用此属性可布局约束的时候有一定的margin,它可以避免在每一个子视图上应用边距

self.view.layoutMargins = UIEdgeInsets(top: self.view.layoutMargins.top,                                       left: 64,                                       bottom: self.view.layoutMargins.bottom,                                       right: 64)

在iOS11中layoutMargins被废弃了,其被UIView的directionalLayoutMargins取代

self.view.directionalLayoutMargins = NSDirectionalEdgeInsets(top: self.view.directionalLayoutMargins.top,                                                             leading: 16,                                                             bottom: self.view.directionalLayoutMargins.bottom,                                                             trailing: 64)

UIViewlayoutMarginsDidChange方法,在view的margin改变后得到通知,或者UIViewControllerviewLayoutMarginsDidChange方法


edgesForExtendedLayout等相关属性

在iOS7以后UIViewController使用全屏布局,默认全屏布局,可参考中介绍

如下的代码,状态栏透明与不透明的区别:

//[self.navigationController.navigationBar setTranslucent:NO];    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];    redView.backgroundColor = [UIColor redColor];    [self.view addSubview:redView];

透明

不透明


1.如何在storyboard中添加一个view container?

参考

2.使用Autolayout来切换Child View Controllers

参考:

3.在iOS Container View中传递数据?

参考

主要是使用prepareForSegue方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){    if (segue.identifier == "myEmbeddedSegue") {        let childViewController = segue.destinationViewController as! myChildViewController        // Now you have a pointer to the child view controller.         // You can save the reference to it, or pass data to it.    }}

View Controller 容器

在讲了个,

使用- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion 做转场动画的例子:

如下的翻转:

这里写图片描述

主要代码如下:

- (void) flipFromViewController:(UIViewController*) fromController toViewController:(UIViewController*) toController  withDirection:(UIViewAnimationOptions) direction andDelay:(double) delay{    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){        toController.view.frame = fromController.view.bounds;                           //  1        [self addChildViewController:toController];                                     //  2        [fromController willMoveToParentViewController:nil];                            //  3        [self transitionFromViewController:fromController                          toViewController:toController                                  duration:0.2                                   options:direction | UIViewAnimationOptionCurveEaseIn                                animations:nil                                completion:^(BOOL finished) {                                    [toController didMoveToParentViewController:self];  //  4                                    [fromController removeFromParentViewController];    //  5                                    [infoButton setEnabled:YES];                                }];    });}

还可以自定义转场动画,如

弹出模态ViewController

以前总没有注意到还可以设置ViewController的Presentation Styles(弹出风格)和Modal Transition Style(弹出时的动画风格)。有详细的解释。

转载地址:https://windzen.blog.csdn.net/article/details/68066531 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:UICollectionView拾遗
下一篇:自定义UICollectionView布局

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月02日 12时08分48秒