zoukankan      html  css  js  c++  java
  • ViewController类中得方法和属性的用途

     

    1、 wantsFullScreenLayout

    只要在UIViewController上设置wantsFullScreenLayout=true ,那么状态栏的高度就不会被算在视图里,也就是说有没有状态栏y坐标始终都是从0算起。 如果没有设置这个的话,旋转屏幕并且隐藏显示状态栏(statusBarHidden)时会出现20像素的白边或者,view跑出屏幕20像素。(这个属性在IOS7中弃用 edgesForExtendedLayout代替)

     

    @property(nonatomic,assign) BOOL wantsFullScreenLayout NS_DEPRECATED_IOS(3_0, 7_0); // Deprecated in 7_0, Replaced by the following:

     

    @property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll

     

    2、edgesForExtendedLayout

    1. 使用 edgesForExtendedLayout 指定视图的哪条边需要扩展,不用理会操作栏的透明度。这个属性的默认值是UIRectEdgeAll。

    2. extendedLayoutIncludesOpaqueBars

      如果你使用了不透明的操作栏,设置 edgesForExtendedLayout 的时候也请将extendedLayoutIncludesOpaqueBars 的值设置为No(默认值是YES)。

     

    3、automaticallyAdjustsScrollViewInsets

    YES时,它会找view里的scrollView,并设置scrollView的contentInset为{64, 0, 0, 0}。如果你不想让scrollView的内容自动调整,将这个属性设为NO(默认值YES)。

    当我们在一个UIViewController中同时创建2个tableView的时候,如果把它们的frame中的Y坐标设置为一样,你可能会发现它们的位置并没有达到你想要的结果.比如第一tableView个frame(0,0,320,568),另一个也frame(0,0,320,568),结果会发现第二个tableView的第一行数据被导航栏遮挡了,以至于我们不得已把第二个frame改成(0,64,320,568-64),虽然效果变成了我们想要的,但是却不知道这是什么原因.......

    其实这一切都是automaticallyAdjustsScrollViewInsets在作怪,我们可以先看一下官方文档中对它的描述:

    automaticallyAdjustsScrollViewInsets

    Specifies whether or not the view controller should automatically adjust its scroll view insets.

    @property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets

    Discussion

    Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set toNO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

    Availability

    • Available in iOS 7.0 and later.

    Declared In

    UIViewController.h

     

    由此可见,当我们一个界面有多个tableView之类的,要将它设置为NO,完全由自己手动来布局,就不会错乱了. 

     

    4、 addChildViewController

    苹果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用[self addChildViewController:child]方法将sub view对应的viewController也加到当前ViewController的管理中。对于那些当前暂时不需要显示的subview,只通过addChildViewController把subViewController加进去。需要显示时再调用transitionFromViewController:toViewController:duration:options:animations:completion方法。

    另外,当收到系统的Memory Warning的时候,系统也会自动把当前没有显示的subview unload掉,以节省内存。

     

    - (BOOL)isViewLoaded 

    判断view是否已加载

     

     

     //Note that as of 5.0 this no longer will return the presenting view controller.

    返回父视图控制器,5.0后不再返回模态框的视图控制器

    @property(nonatomic,readonly) UIViewController *parentViewController;

     

    // 这个属性被presentedViewController取代

    @property(nonatomic,readonly) UIViewController *modalViewController NS_DEPRECATED_IOS(2_0, 6_0);

     

    // The view controller that was presented by this view controller or its nearest ancestor.

    返回被弹出的视图控制器

    @property(nonatomic,readonly) UIViewController *presentedViewController  NS_AVAILABLE_IOS(5_0);

     

    // The view controller that presented this view controller (or its farthest ancestor.)

    返回需要弹出的视图控制器

    @property(nonatomic,readonly) UIViewController *presentingViewController NS_AVAILABLE_IOS(5_0);

     当我们在view controller A中模态显示view controller B的时候,A就充当presenting view controller(弹出VC),而B就是presented view controller(被弹出VC)。官方文档建议这两者之间通过delegate实现交互

     

     

    @property(nonatomic,assignUIModalTransitionStyle modalTransitionStyle

    Modal Transition Style(弹出时的动画风格)

      通过设置设置presented VC的modalTransitionStyle属性,我们可以设置弹出presented VC时场景切换动画的风格,其定义如下:

    typedef enum {
            UIModalTransitionStyleCoverVertical = 0,
            UIModalTransitionStyleFlipHorizontal,
            UIModalTransitionStyleCrossDissolve,
            UIModalTransitionStylePartialCurl,
    } UIModalTransitionStyle;

      我们可以看到有从底部滑入,水平翻转进入,交叉溶解以及翻页这四种风格可选。这四种风格在不受设备的限制,即不管是iPhone还是iPad都会根据我们指定的风格显示转场效果。

     

    @property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle

    Modal Presentation Styles(弹出风格)

      通过设置presented VC的modalPresentationStyle属性,我们可以设置弹出View Controller时的风格,有以下四种风格,其定义如下:

    typedef enum {
        UIModalPresentationFullScreen = 0,
        UIModalPresentationPageSheet,
        UIModalPresentationFormSheet,
        UIModalPresentationCurrentContext,
    } UIModalPresentationStyle;

      UIModalPresentationFullScreen代表弹出VC时,presented VC充满全屏,如果弹出VC的wantsFullScreenLayout设置为YES的,则会填充到状态栏下边,否则不会填充到状态栏之下。

      UIModalPresentationPageSheet代表弹出是弹出VC时,presented VC的高度和当前屏幕高度相同,宽度和竖屏模式下屏幕宽度相同,剩余未覆盖区域将会变暗并阻止用户点击,这种弹出模式下,竖屏时跟UIModalPresentationFullScreen的效果一样,横屏时候两边则会留下变暗的区域。

      UIModalPresentationFormSheet这种模式下,presented VC的高度和宽度均会小于屏幕尺寸,presented VC居中显示,四周留下变暗区域。

      UIModalPresentationCurrentContext这种模式下,presented VC的弹出方式和presenting VC的父VC的方式相同。

      这四种方式在iPad上面统统有效,但在iPhone和iPod touch上面系统始终已UIModalPresentationFullScreen模式显示presented VC。

    preferredContentSize

    1. // From UIContentContainer Protocol  
    2. @property (nonatomic, readonly) CGSize preferredContentSize NS_AVAILABLE_IOS(8_0);  
    3. - (void)preferredContentSizeDidChangeForChildContentContainer:(id )container NS_AVAILABLE_IOS(8_0);  
    4.  
    5. // From UIViewController  
    6. @property (nonatomic) CGSize preferredContentSize NS_AVAILABLE_IOS(7_0); 

    preferredContentSize在UIContentContainer协议中是只读的,对应的UIViewController有可写的版本。我们可以使用preferredContentSize来设置我们期望的ChildViewController的界面大小。举个例子,如果应用中使用的popOver大小会发生变化,iOS7之前我们可以用contentSizeForViewInPopover来调整。iOS7开始这个API被废弃,我们可以使用preferredContentSize来设置。

    当一个容器ViewController的ChildViewController的这个值改变时,UIKit会调用preferredContentSizeDidChangeForChildContentContainer这个方法告诉当前容器ViewController。我们可以在这个方法里根据新的Size对界面进行调整。

    - (UIStatusBarStyle)preferredStatusBarStyle

    在你自己的UIViewController里重写此方法,返回你需要的值(UIStatusBarStyleDefault 或者 UIStatusBarStyleLightContent);

    注意:

    • 这里如果你只是简单的return一个固定的值,那么该UIViewController显示的时候,程序就会马上调用该方法,来改变statusBar的前景部分;
    • 如果在该UIViewController已经在显示在当前,你可能还要在当前页面不时的更改statusBar的前景色,那么,你首先需要调用下面的setNeedsStatusBarAppearanceUpdate方法(这个方法会通知系统去调用当前UIViewController的preferredStatusBarStyle方法), 这个和UIView的setNeedsDisplay原理差不多(调用UIView对象的setNeedsDisplay方法后,系统会在下次页面刷新时,调用重绘该view,系统最快能1秒刷新60次页面,具体要看程序设置)。

    - (BOOL)prefersStatusBarHidden

    //隐藏状态栏

    IOS7中,不仅应用的风格有一定的变化,状态栏变化比较大,我们可以看到UIVIEWCONTROLLER的状态栏与导航栏基本是一体的。因此UIVIEWCONTROLLER的HIDE/SHOW状态的方法也跟其他版本的不一样了。 在IOS7以前的版本,HIDE/SHOW是通过以下代码实现

    [[UIApplication sharedApplication] setStatusBarHidden:YES(NO) withAnimation:UIStatusBarAnimationSlide];  


    在iOS7中默认情况下,这个方法不成功了。到setStatusBarHidden:withAnimation:声明的头文件去看看,多了一句注释: // Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system. 现在在iOS7中,status bar的外观默认依赖UIViewController, 也就是说status bar随UIViewController的不同而不同。在这种默认的方式下,用全局的方法setStatusBarHidden:withAnimation:是行不通的。

    google一下发现现在的解决方法有两种:

    如果只是单纯的隐藏状态栏,那么是在默认情况下,只需要重新实现两个新方法

    1. - (UIStatusBarStyle)preferredStatusBarStyle  
    2. {  
    3.     return UIStatusBarStyleLightContent;  
    4.     //UIStatusBarStyleDefault = 0 黑色文字,浅色背景时使用  
    5.   //UIStatusBarStyleLightContent = 1 白色文字,深色背景时使用  
    6. }  
    7.   
    8. - (BOOL)prefersStatusBarHidden  
    9. {  
    10.     return NO; //返回NO表示要显示,返回YES将hiden  
    11. }  

    上面一个回调方法返回status bar显示时候的样式,下面一个回调控制是否显示status bar.

    调用下面的一行代码将会触发上面的回调

    [self setNeedsStatusBarAppearanceUpdate];  

    如果想在hiden/show之间有点动画效果,用下面的代码即可:

    1. [UIView animateWithDuration:0.5 animations:^{  
    2.         [self setNeedsStatusBarAppearanceUpdate];  
    3.     }];  

    或者调用下面的代码:

    1. [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];  

     

    - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation

    现在有两种动画效果:UIStatusBarAnimationFade ,UIStatusBarAnimationSlide.从他们的名字可以得知他的具体是什么眼的效果。 实现动画需要重载:

    1
    2
    3
    4
    - (UIStatusBarAnimation )preferredStatusBarUpdateAnimation
    {
        return UIStatusBarAnimationSlide;
    }

     但是重载这个方法后,并没有动画效果。这时,我们需要把 [self setNeedsStatusBarAppearanceUpdate] 放在动画block中执行:

    1
    2
    3
    4
    [UIView animateWithDuration:0.3
                     animations:^{
                        [self setNeedsStatusBarAppearanceUpdate];
    }];

    这样动画效果就出现了。

     

    // This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.

    - (void)setNeedsStatusBarAppearanceUpdate

    //每当statusbar的属性改变的时候,这个函数就应该被调用,更新这个状态。如果在动画的block中调用,就会有动画效果。

     

     

    // An array of children view controllers. This array does not include any presented view controllers.

    保存子视图控制器的数组

    @property(nonatomic,readonly) NSArray *childViewControllers NS_AVAILABLE_IOS(5_0);

     

    苹果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用[self addChildViewController:child]方法将sub view对应的viewController也加到当前ViewController的管理中。对于那些当前暂时不需要显示的subview,

    只通过addChildViewController把subViewController加进去。需要显示时再调用transitionFromViewController:toViewController:duration:options:animations:completion方法。

     

    //添加、移除子控制器

    - (void)addChildViewController:(UIViewController *)childController

    - (void) removeFromParentViewController

     

     

    - (UIViewController *)childViewControllerForStatusBarStyle NS_AVAILABLE_IOS(7_0);

    这个接口也很重要,默认返回值为nil。当我们调用setNeedsStatusBarAppearanceUpdate时,系统会调用application.window的rootViewController的preferredStatusBarStyle方法,我们的程序里一般都是用UINavigationController做root,如果是这种情况,那我们自己的UIViewController里的preferredStatusBarStyle根本不会被调用; 
    这种情况下childViewControllerForStatusBarStyle就派上用场了, 
    我们要子类化一个UINavigationController,在这个子类里面重写childViewControllerForStatusBarStyle方法,如下:

    - (UIViewController *)childViewControllerForStatusBarStyle{
        return self.topViewController;
    }
    

    上面代码的意思就是说,不要调用我自己(就是UINavigationController)的preferredStatusBarStyle方法,而是去调用navigationController.topViewControllerpreferredStatusBarStyle方法,这样写的话,就能保证当前显示的UIViewController的preferredStatusBarStyle方法能影响statusBar的前景部分。

    另外,有时我们的当前显示的UIViewController可能有多个childViewController,重写当前UIViewController的childViewControllerForStatusBarStyle方法,让childViewController的preferredStatusBarStyle生效(当前UIViewController的preferredStatusBarStyle就不会被调用了)。

    简单来说,只要UIViewController重写的的childViewControllerForStatusBarStyle方法返回值不是nil,那么,UIViewController的preferredStatusBarStyle方法就不会被系统调用,系统会调用childViewControllerForStatusBarStyle方法返回的UIViewController的preferredStatusBarStyle方法。

    - (void)setNeedsStatusBarAppearanceUpdate:

    让系统去调用application.window的rootViewController的preferredStatusBarStyle方法,如果rootViewController的childViewControllerForStatusBarStyle返回值不为nil,则参考上面的讲解。

    - (void)loadViewIfNeeded NS_AVAILABLE_IOS(9_0);

    使用如下代码进行UIViewController之间的跳转:

     TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];

     //[testViewController loadViewIfNeeded];

     testViewController.imageViewCourse.image = image;

    testViewController.lbCourse.text = course;

     [self presentViewController:testViewController animated:YES completion:nil];

    我们使用nib来加载一个TestViewController并对其属性赋值, 然后跳转。问题在于执行完initWithNibName之后,testViewController.imageViewCourse和testViewController.lbCourse都为nil, 则表现出来的是跳转到TestViewController之后, 其中的imageViewCourse和lbCourse中没有内容。

    我们知道,当我们从StoryBoard中加载ViewController时,我们在Controller中拖拽的视图是可以被初始化的,这里面有一点需要我们注意,如果我们需要向controller中视图进行传值设置,通过以下方法得到的Controller中,视图还没有被初始化创建出来,可以想象,如果我们这时候需要对label进行一些属性设置,必然失败。有人提出可以在创建后,手动调以下loadView方法,我们试一下,结果如下:可以看到,手动调用loadView后,label是被创建了出来,但是暴漏了一个更严重的问题,系统不在调用ViewDidLoad方法,这是十分有风险的,因为我们大部分的初始化代码都会放在这个方法里,所以手动调用loadView是一种错误的方法,apple文档声明对于loadView方法,我们从来都不要手动直接调用,那么我们如何实现创建后对成员对象进行传值设置呢,iOS9中增加了这样一个方法:loadViewIfNeeded

    这个方法十分有用,调用这个方法,会将视图创建出来,并且不会忽略viewDidLoad的调用。

    iOS9中,UIViewController还增加了下面一个布尔值的属性,可以同来判断controllerview是否已经加载完成:

    @property(nullable, nonatomic, readonly, strong) UIView *viewIfLoaded NS_AVAILABLE_IOS(9_0);

  • 相关阅读:
    数据库sql一些常考基础命令
    C# and .NET之父——传奇的anders hejlsberg
    数据库中3大范式的详解
    小胖求职记—求职中的技巧
    程序员们喜爱看的文章
    为什么世界上没有天才(转)
    C语言中system函数的使用
    关于DataGrid的知识和技巧
    分页
    在调用方法时产生异常的处理方法
  • 原文地址:https://www.cnblogs.com/HypeCheng/p/4686645.html
Copyright © 2011-2022 走看看