alloc init
//這個(gè) 方法是在viewdidload之前加載的震嫉,以nib文件加載必然會(huì)調(diào)用此方法,但是代碼加載也是會(huì)自動(dòng)調(diào)用的审残,因此可以將一些數(shù)組創(chuàng)建秸抚,標(biāo)題命名等初始化操作寫(xiě)在這里
//1.如果在初始化UIViewController指定了xib文件名掰吕,就會(huì)根據(jù)傳入的xib文件名加載對(duì)應(yīng)的xib文件
//2.如果沒(méi)有明顯地傳xib文件名([[MJViewController alloc] init];),就會(huì)加載跟UIViewController同名的xib文件
//3.如果沒(méi)有找到相關(guān)聯(lián)的xib文件,就會(huì)創(chuàng)建一個(gè)空白的UIView涛碑,然后賦值給UIViewController的view屬性
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil NS_DESIGNATED_INITIALIZER;
//NSCoding協(xié)議
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
//這個(gè)UIViewController的nib名字
@property(nullable, nonatomic, readonly, copy) NSString *nibName;
//這個(gè)UIViewController的nibBundle
@property(nullable, nonatomic, readonly, strong) NSBundle *nibBundle;
//這個(gè)UIViewControlle所在的Storyboar
@property(nullable, nonatomic, readonly, strong) UIStoryboard *storyboard NS_AVAILABLE_IOS(5_0);
loadView
//如果獲取self.view,如果沒(méi)有現(xiàn)實(shí)[self loadView]嗜暴,就是調(diào)用Subclasses的loadView方法凸克。
@property(null_resettable, nonatomic,strong) UIView *view;
//loadView里面自定義self.view
- (void)loadView;
//iOS9之前
//有些時(shí)候因?yàn)樾枰謩?dòng)調(diào)用loadview
//但是有風(fēng)險(xiǎn),系統(tǒng)不再調(diào)用viewDidLoad
//所以手動(dòng)調(diào)用loadview是錯(cuò)誤的
//iOS9之后
//出現(xiàn)了loadViewIfNeeded解決了這個(gè)問(wèn)題
//調(diào)用這個(gè)方法視圖會(huì)創(chuàng)建出來(lái)并且不會(huì)忽略viewDidLoad
- (void)loadViewIfNeeded NS_AVAILABLE_IOS(9_0);
//返回加載完的self.view
@property(nullable, nonatomic, readonly, strong) UIView *viewIfLoaded NS_AVAILABLE_IOS(9_0);
//view是否加載完成
@property(nonatomic, readonly, getter=isViewLoaded) BOOL viewLoaded ;
- (BOOL)isViewLoaded ;
Storyboary相關(guān)
//Storyboar跳轉(zhuǎn)擺好的界面
//identifier:設(shè)置好的界面連線(就是Storyboar中VC與VC那條線)
//sender:發(fā)送者
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender NS_AVAILABLE_IOS(5_0);
//看看能不能跳轉(zhuǎn)闷沥,主要是看identifier對(duì)不對(duì)
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender NS_AVAILABLE_IOS(6_0);
//準(zhǔn)備處理萎战,
//segue:用來(lái)判斷是哪個(gè)segue,里面有identifier舆逃、sourceViewController蚂维、destinationViewController可以各種操作
//sender:發(fā)送者
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender NS_AVAILABLE_IOS(5_0);
//Unwind Segue相關(guān),(不明白路狮,留著慢慢玩)
- (BOOL)canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender NS_AVAILABLE_IOS(6_0);
- (NSArray<UIViewController *> *)allowedChildViewControllersForUnwindingFromSource:(UIStoryboardUnwindSegueSource *)source NS_AVAILABLE_IOS(9_0);
- (nullable UIViewController *)childViewControllerContainingSegueSource:(UIStoryboardUnwindSegueSource *)source NS_AVAILABLE_IOS(9_0);
- (nullable UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(nullable id)sender NS_DEPRECATED_IOS(6_0, 9_0);
- (void)unwindForSegue:(UIStoryboardSegue *)unwindSegue towardsViewController:(UIViewController *)subsequentVC NS_AVAILABLE_IOS(9_0);
- (nullable UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(nullable NSString *)identifier NS_DEPRECATED_IOS(6_0, 9_0);
生命周期函數(shù)
//系統(tǒng)的loadview完成后虫啥,執(zhí)行viewDidLoad
- (void)viewDidLoad;
//這幾個(gè)生命周期方法,必須熟悉
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0);
- (void)didReceiveMemoryWarning;
單個(gè)viewController的生命周期
1奄妨、initWithCoder:(NSCoder *)aDecoder:(如果使用storyboard或者xib)
2涂籽、loadView:加載view
3、viewDidLoad:view加載完畢
4砸抛、viewWillAppear:控制器的view將要顯示
5评雌、viewWillLayoutSubviews:控制器的view將要布局子控件
6、viewDidLayoutSubviews:控制器的view布局子控件完成锰悼,這期間系統(tǒng)可能會(huì)多次調(diào)用viewWillLayoutSubviews 柳骄、 viewDidLayoutSubviews 倆個(gè)方法
7、viewDidAppear:控制器的view完全顯示
8箕般、viewWillDisappear:控制器的view即將消失的時(shí)候
9耐薯、這期間系統(tǒng)也會(huì)調(diào)用viewWillLayoutSubviews 、viewDidLayoutSubviews 兩個(gè)方法
10、viewDidDisappear:控制器的view完全消失的時(shí)候
多個(gè)viewControllers跳轉(zhuǎn)
當(dāng)我們點(diǎn)擊push的時(shí)候首先會(huì)加載下一個(gè)界面然后才會(huì)調(diào)用界面的消失方法
1曲初、initWithCoder:(NSCoder *)aDecoder:ViewController2 (如果用xib創(chuàng)建的情況下)
2体谒、loadView:ViewController2
3、viewDidLoad:ViewController2
4臼婆、viewWillDisappear:ViewController1 將要消失
5抒痒、viewWillAppear:ViewController2 將要出現(xiàn)
6、viewWillLayoutSubviews ViewController2
7颁褂、viewDidLayoutSubviews ViewController2
8故响、viewWillLayoutSubviews:ViewController1
9、viewDidLayoutSubviews:ViewController1
10颁独、viewDidDisappear:ViewController1 完全消失
11彩届、viewDidAppear:ViewController2 完全出現(xiàn)
//nav標(biāo)題
@property(nullable, nonatomic,copy) NSString *title;
@property(nullable,nonatomic,weak,readonly) UIViewController *parentViewController;//父控制器
@property(nullable, nonatomic,readonly) UIViewController *presentedViewController;//第二者
@property(nullable, nonatomic,readonly) UIViewController *presentingViewController;//第一者
//例子
UIViewController *vcB = [[UIViewController alloc] init];
[self addChildViewController:vcB];
NSLog(@"%@", vcB.parentViewController);//self
UIViewController *vcB = [[UIViewController alloc] init];
[self presentViewController:vcB animated:YES completion:^{}];
NSLog(@"%@", self.presentedViewController);//vcB
NSLog(@"%@", self.presentingViewController);//nil
NSLog(@"%@", vcB.presentedViewController);//nil
NSLog(@"%@", vcB.presentingViewController);//self
//UIViewController的edgesForExtendedLayout屬性默認(rèn)值是UIRectEdgeAll,指定控制器將它的視圖延伸到屏幕的邊緣并在bar下面誓酒。如果屬性值為:UIRectEdgeNone樟蠕,控制器視圖遇到bar的邊界就不延伸了。
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout ; // Defaults to UIRectEdgeAll
//UIViewController的extendedLayoutIncludesOpaqueBars屬性可以控制以上屬性的有效性靠柑,默認(rèn)值為NO,指定edgesForExtendedLayout在遇到不透明的bar時(shí)無(wú)效寨辩,即不延展。設(shè)置值為YES歼冰,則在遇到透明或不透明的bar情況下都會(huì)延展靡狞。
@property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars ; // Defaults to NO, but bars are translucent by default on 7_0.
//UIViewController的automaticallyAdjustsScrollViewInsets默認(rèn)為YES,指定控制器在有UIScrollView及其子類(lèi)并且在有導(dǎo)航欄或工具欄或標(biāo)簽欄情況下,會(huì)自動(dòng)調(diào)整其contentInset屬性隔嫡。如果是導(dǎo)航欄contentInset.top = 64,如果是標(biāo)簽欄contentInset.bottom = 44.
//可以將該屬性設(shè)置為NO,取消這種行為耍攘。
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets ; // Defaults to YES
UIStatusBar相關(guān)的設(shè)置iOS-UIStatusBar詳細(xì)總結(jié)
- (UIStatusBarStyle)preferredStatusBarStyle ;
- (BOOL)prefersStatusBarHidden ;
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
UIModalTransitionStyle是彈出模態(tài)ViewController時(shí)的四種動(dòng)畫(huà)風(fēng)格
typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
UIModalTransitionStyleCoverVertical = 0,//是從底部滑入,默認(rèn)
UIModalTransitionStyleFlipHorizontal , //是水平翻轉(zhuǎn)
UIModalTransitionStyleCrossDissolve, //是交叉溶解
UIModalTransitionStylePartialCurl, //是翻書(shū)效果
};
UIModalPresentationStyle是彈出模態(tài)ViewController時(shí)彈出風(fēng)格
Present ViewController詳解
typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
UIModalPresentationFullScreen = 0, //是彈出VC時(shí),VC充滿全屏
UIModalPresentationPageSheet , //是如果設(shè)備橫屏畔勤,VC的顯示方式則從橫屏下方開(kāi)始
UIModalPresentationFormSheet , //是VC顯示都是從底部,寬度和屏幕寬度一樣
UIModalPresentationCurrentContext , //是VC的彈出方式和VC父VC的彈出方式相同
UIModalPresentationCustom , //自定義
UIModalPresentationOverFullScreen , //
UIModalPresentationOverCurrentContext , //
UIModalPresentationPopover , //
UIModalPresentationNone , //
};
如何present一個(gè)半透明的ViewController
- (void)btnOnClick:(id)sender {
UIViewController *test = [[UIViewController alloc] init];
self.definesPresentationContext = YES;
test.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];
test.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:test animated:YES completion:nil];
}
UIContentContainer
iOS8之后扒磁,加入了新的一組協(xié)議庆揪,UIViewController對(duì)這組協(xié)議提供了默認(rèn)的實(shí)現(xiàn),我們自定義ViewConttroller的時(shí)候可以重寫(xiě)這些方法來(lái)調(diào)整視圖布局妨托。(先放放缸榛,等有時(shí)間在研究使用)
@protocol UIContentContainer <NSObject>
@property (nonatomic, readonly) CGSize preferredContentSize NS_AVAILABLE_IOS(8_0);
//當(dāng)一個(gè)容器ViewController的ChildViewController的preferredContentSize值改變時(shí),UIKit會(huì)調(diào)用這個(gè)方法告訴當(dāng)前容器ViewController兰伤。我們可以在這個(gè)方法里根據(jù)新的Size對(duì)界面進(jìn)行調(diào)整内颗。
- (void)preferredContentSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container NS_AVAILABLE_IOS(8_0);
//當(dāng)一個(gè)容器ViewController的ChildViewController的systemLayoutFittingSize值改變時(shí),UIKit會(huì)調(diào)用這個(gè)方法告訴當(dāng)前容器ViewController敦腔。我們可以在這個(gè)方法里根據(jù)新的Size對(duì)界面進(jìn)行調(diào)整均澳。
- (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container NS_AVAILABLE_IOS(8_0);
//viewWillTransitionToSize:withTransitionCoordinator:調(diào)用的時(shí)候
- (CGSize)sizeForChildContentContainer:(id <UIContentContainer>)container withParentContainerSize:(CGSize)parentSize NS_AVAILABLE_IOS(8_0);
//TransitionToSize:動(dòng)畫(huà)方面觸發(fā)?
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
//什么鬼啊找前?
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
@end