UIContainerView純代碼實現及原理介紹

UIContainerView純代碼實現及原理介紹

1.1-在StoryBoard中使用UIContainerView

  • 1.在storyboard中搜索UIContainerview並拖入到控制器中,設置約束

這裏寫圖片描述

  • 2.能夠看到ContainerView自帶一個segue連線的控制器,而這個Segue既不是Push跳轉而不是Model跳轉,而是Embed嵌入的形式

這裏寫圖片描述

  • 3.運行,能夠看到右側控制器的視圖經過UIContainerView嵌入到左側的控制器中

這裏寫圖片描述

1.2-純代碼使用UIContainerView

  • 1.建立一個控制器,設置背景顏色爲綠色

這裏寫圖片描述

  • 2.將Storyboard中的UIContainerView拖到代碼時,會發現根本沒有UIContainerview這個類,它的本質其實就是一個UIView 
    • 本人推測:它本質上應該是一個未開放的UIView的一個Category分類

這裏寫圖片描述

  • 3.純代碼實現UIContainerview

這裏寫圖片描述

#import "ViewController.h" #import "SecondViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *containerView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self addContainerView]; // Do any additional setup after loading the view, typically from a nib. } - (void)addContainerView { //1.建立containerView目標控制器 SecondViewController *second = [[SecondViewController alloc] init]; //設置背景顏色 second.view.backgroundColor = [UIColor greenColor]; //2.將目標控制器的視圖賦值給容器視圖(不能用addSubView,不然設置frame無效) self.containerView = second.view; //設置顯示大小 self.containerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-200); //3.添加到當前視圖 [self.view addSubview:self.containerView]; //4.獲取到Containerview的目標控制器 NSLog(@"%@",self.containerView.nextResponder); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

1.3-UIContainerview特色及原理分析

  • UIContainView是iOS系統中很是特殊的一個控件,它的主要特色以下:ios

    • (1)只能在Storyboard中搜索到UIContainerview,代碼中沒有這個類 
      • 應該是屬於系統並未開放的UIView的分類
    • (2)是一個不可以被渲染的視圖容器 
      • 不能被渲染,也就是說設置背景顏色backgroundColor和bounds屬性等一些渲染屬性是無效的
  • UIContainerview的原理markdown

    • UIContainer經過強引用控制器的視圖(賦值操做),再做爲其餘控制器的子視圖,從而達到一個控制器中顯示兩個控制器視圖的效果
 
0
轉自:http://blog.csdn.net/u013263917/article/details/53487368
相關文章
相關標籤/搜索