
本文共 4689 字,大约阅读时间需要 15 分钟。
UIViewController相关
iOS7中引入automaticallyAdjustsScrollViewInsets
,让系统根据是否存在顶部或底部导航元素(UINavigationBar类型)自动计算scrollview的inset,该属性默认设置为true
automaticallyAdjustsScrollViewInsets
现已废弃,被UIScrollView
的contentInsetAdjustmentBehavior
所代替,可参考:
UIViewController自定义导航栏,可参考:
在iOS11中UIViewController的topLayoutGuide
和bottonLayoutGuide
的两个属性被废弃,被UIView的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改变时,得到通知,可使用UIView
的safeAreaInsetsDidChange
方法,或者UIViewController
的viewSafeAreaInsetsDidChange
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)
UIView
的layoutMarginsDidChange
方法,在view的margin改变后得到通知,或者UIViewController
的viewLayoutMarginsDidChange
方法
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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关于作者
