自定义View(视图)的创建、添加、移除细节分析

 

 

 

1.创建自定义view的类文件

 

计算机生成了可选文字:Choose options for your new file: Class: Subclass of: Language: Cancel YCCustomView UlView —J Also create XIB file iPhone Objective-C Previous Next

 

 

2.创建一个xib文件来表述自定义view(也可以通过代码)

 

  • 创建一个xib文件

计算机生成了可选文字:Choose a template for your new file: View 1 Launch Screen Empty iOS Source User Interface Core Data Resource Other Apple Watch Source User Interface Core Data Resource Other Cancel Storyboard Application View Window Previous An Interface Builder document for creating an iOS view.

 

 

  • 设置xib文件名

计算机生成了可选文字:Favorites Recents O iCloud Drive Sunshine Devices Macintosh HD2 DATA O Tags New Folder Save As: Tags: 000 000 YCCustomView h m h m h m Q Search AppDelegate.h AppDelegate.m Base.lproj Images.xcassets Info.plist main.m ViewController.h ViewController.m YCCustomView.h YCCustomView.m Cancel Create •••iß.xcodeproj Group Targets

 

 

  • 编辑xib文件(绑定class、添加子控件)

计算机生成了可选文字:YCCustomView.xib Custom View Label Button Custo Class YCCustomView Module None Identity Restoration ID User Defined Runtime Attributes O Placeholders File's Owner First Responder Custom View Label Button Switch Round Style Text Field 2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m h ViewController.h m ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files h YCCustomView.h YCCustomView.m YCCustomView.xib iOS Products Key Path Type Value Document Label Object ID Lock Notes Accessibility Accessibility Label -)EißTests Xcode Specific Label x iNO-13-epB Inherited - (Nothing) No Font n Enabled

 

 

  • 声明一个类方法,快速创建一个自定义的view

 

计算机生成了可选文字:YCCustomView. h Created by Sunshine on 15/6/27. Copyright (c) 2Ø15# All #import «UIKit/UIKit. YCCustomView.h rese rved. No Selection 2 3 5 6 9 10 rights 2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m h ViewController.h m ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files YCCustomView.h m YCCustomView.m YCCustomView.xib Products 11 @interface YCCustomView : UlView instancetype)customView;

 

  • 类方法的实现

 

计算机生成了可选文字:YCCustomView. m Created by Sunshine on 15/6/27. Copyright (c) 2Ø15# All #import "YCCustomView. h" YCCustomView.m @implementation YCCustomView rese rved. 2 3 5 6 9 10 2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m h ViewController.h m ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files h YCCustomView.h YCCustomView.m YCCustomView.xib Products rights owner: nil options: nil ø 11|@implementation YCCustomView instancetype)customView return NSBundle mainBundle 18.1 @end loadNibNamed

 

 

 

  • 创建一个自定义view并添加到控制器的view上面

计算机生成了可选文字:hidden YES hidden YES 375 667); autoresize W 15 16 17 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 38 39 41 42 43 ) I ) m Viewcontroller.m ) -viewDidLoad @implementation ViewCont rolle r iOS Simulator - iPhone 6 - iPh... Label 2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m ViewController.h m ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files h YCCustomView.h m YCCustomView.m YCCustomView.xib Products 18 - ( void) viewDidLoad super viewDidLoad *customView LYCCustomView customView) ; YCCustomView // frame, customView. f rame self. view. bounds • customView. backg roundColor UIColor redColor self. view addSubview: customViewI ; NSLog for UIView NSLog self. view. subviews. count) ; *view in self. view. subviews , view); void)didReceiveMemoryWarning super didReceiveMemoryWa rning // Dispose of any resources that can be recreated. layer - layer - +H; layer «CALayer: 2ø15-ø6-27 3 2ø15-ø6-27 - «CALayer: Øx7f8e8b1Ø4f2Ø» 2ø15-ø6-27 - «CALayer: Øx7f8e8b1Ø54bØ» 2ø15-ø6-27 «_UILayoutGuide: Øx7f8e8b1Ø627Ø; frame — «_UILayoutGuide: Øx7f8e8b1Ø6c2Ø; frame — YCCustomView: Øx7f8e88d121fØ; frame —

 

  • 从控制器的view(父控件)上面移除自定义的view(子控件)

 

计算机生成了可选文字:hidden YES hidden YES 15 16 17 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 38 39 41 42 43 @implementation ViewCont ro Ile r m ViewController.m ) customView) ; -viewDidLoad iOS Simulator - iPhone 6 - iPh... $.42 PM .storyboard 2 targets, iOS SDK 8.3 Carrier 18 - ( void)viewDidLoad super viewDidLoad *customView LYCCustomView YCCustomView h h h AppDelegate.h AppDelegate.m ViewController.h ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files YCCustomView.h YCCustomView.m YCCustomView.xib // frame, customView. f rame self. view. bounds customView. backg roundColor UlC010r self. view addSubview: customViewI ; lÉJ s e If. view redColor Products frame — frame — (customView NSLog for UIView NSLog removeF romSupe rv iew self. view. subviews. count) ; *view in self. view. subviews , view); )didReceiveMemoryWarning void 2ø15-ø6-27 16:41:57.966 2ø15-ø6-27 16:41:57.966 layer - — «CALayer: Øx7fbd815123cØ» 2ø15-ø6-27 16:41:57.967 layer - — «CALayer: Øx7fbd81521c5Ø» 2ø15-ø6-27 16:41:57.972 2 • 8376621 • 8376621 • 8376621 • 8376621 ILayoutGuide: Øx7fbd81522e6Ø; UlLayoutGuide: Øx7fbd8152381Ø; customVieR%nr

 

 

  • 重写自定义viewdealloc方法,监听其销毁

计算机生成了可选文字:YCCustomView. m Created by Sunshine on 15/6/27. Copyright (c) 2Ø15# All m YCCustomView.m rese rved. @implementation YCCustomView owner: nil options: nil ø 2 3 5 6 9 10 11 12 14 17 20 21 2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m h ViewController.h m ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files YCCustomView.h YCCustomView.m YCCustomView.xib Products rights #import "YCCustomView. h" @implementation YCCustomView instancetype)customView return NSBundle mainBundle void)dealloc NSLog loadNibNamed 23 Cend 24

 

  • 官方对removeFromSuperview方法的说明:

计算机生成了可选文字:removeFromSuperview Unlinks the receiver from its superview and its window, and removes it from the responder chain.

 

计算机生成了可选文字:- (void)removeFromSuperview Discussion If the receiver's superview is not nil, the superview releases the receiver. If you plan to reuse a view, be sure to retain it before calling this method and release it again later as appropriate. Never call this method from inside your view's drawRect: method.

 

 

 

调用顺序详细说明:

 

  • 1.创建自定义view的时候

计算机生成了可选文字:9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 38 39 41 42 ViewController.m No Selection 2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m h ViewController.h ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files h YCCustomView.h m YCCustomView.m YCCustomView.xib Products #import "ViewController. h" #import "YCCustomView. h" @interface ViewCont ro I ler @end @implementation ViewCont ro Ile r void) viewDidLoad super viewDidLoad *customView LYCCustomView customView) ; YCCustomView // frame, customView. f rame self. view. bounds customView. backg roundColor UIColor redColor self. view addSubview: customView) ; / / iew (customView removeF romSupe rv iew void)didReceiveMemoryWarning rec reated . super didReceiveMemoryWa rning // Dispose of any resources that can be Cend

 

 

  • 重写didAddSubview:方法

计算机生成了可选文字:9 10 11 12 13 14 17 20 21 #import "YCCustomView. h" @implementation YCCustomView #pragma mark — jJLIäxibqafiview#-E instancetype)customView return NSBundle mainBundle) owner: nil options YCCustomView.m -didMoveToSuperview 2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m h ViewController.h ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files h YCCustomView.h YCCustomView.m YCCustomView.xib Products 18 mark - void) didAddSubview: ( *)subview UlView NSLog NSLog (subview class func_) ;

 

计算机生成了可选文字:Placeholders File's Owner First Responder Custom View Label Button Switch Round Style Text Field 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 YCCustomView.xib Any hAny w • 1233ø451 UlLabe1 •1233ø451 - • 1233ø451 •1233ø451 - • 1233ø451 •1233ø451 - • 1233ø451 •1233ø451 - No Selection Label Button 2 targets, iOS SDK 8.3 h h h AppDelegate.h AppDelegate.m ViewController.h ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files YCCustomView.h YCCustomView.m YCCustomView.xib 1:13 4 Products (YCCustomView UIButton (YCCustomView UISwitch (YCCustomView UlTextFie1d LYCCustomView didAddSubview: didAddSubview: didAddSubview: didAddSubview:

 

 

 

 

2.添加自定义view的时候

计算机生成了可选文字:m ViewController.m No Selection 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 38 39 41 42 2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m ViewController.h ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files h YCCustomView.h m YCCustomView.m YCCustomView.xib Products #import "ViewController. h" #import "YCCustomView. h" @interface ViewCont ro I ler @end @implementation ViewCont ro Ile r void) viewDidLoad super viewDidLoad *customView LYCCustomView customViewI ; YCCustomView // frame, customView. f rame self. view. bounds • customView. backg roundColor UIColor redColor self. view addSubview: customViewI ; / / iew (customView removeF romSupe rv iew void)didReceiveMemoryWarning rec reated . super didReceiveMemoryWa rning // Dispose of any resources that can be @end

 

 

  • 重写相应的方法,监听其执行顺序

计算机生成了可选文字:2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m h ViewController.h m ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib NSBundle mainBundle) loadNibNamed owner: nil options Supporting Files h YCCustomView.h YCCustomView.m YCCustomView.xib 38 - ( 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 39 #import "YCCustomView. h" @implementation YCCustomView #pragma mark — instancetype)customView retu rn #pragma mark - void) didAddSubview: ( *)subview UlView YCCustomView.m -didMoveToSuperview Products NSLog NSLog Lsubview n; class func_) ; #pragma marl — void )willMoveToSupe rview: ( newSuperview UlView , newSuperview) ; NSLog func_) ; NSLog #pragma mark — void)didMoveToSuperview func_) ; NSLog #pragma mark — %lJUIWindow-EWißE void )wil IMoveToWindow: (UIWindow newWindow , newWindow) ; NSLog func_) ; NSLog #pragma mark — void)didMoveToWindow func_) ; NSLog

 

计算机生成了可选文字:«UIView: Øx7fb37181cfbØ; frame (Ø Ø 2ø15-ø6-27 375 667); autoresize W+H; layer «CALayer: Øx7fb3718173aØ» (YCCustomView willMoveToSuperview: I 2ø15-ø6-27 - LYCCustomView didMoveToSuperviewl 2ø15-ø6-27 - «UIWindow: Øx7fb37Ø78469Ø; frame (ø 2ø15-ø6-27 18:28:29.286 Ø; 375 667); gestureRecognizers «NSArray: Øx7fb37Ø77583b; layer 41WindowLayer: LYCCustomView willMoveToWindow: I 2ø15-ø6-27 18:28:29.286 - (YCCustomView didMoveToWindow) 2ø15-ø6-27 -

 

 

 

 

 

2.从父控件移除自定义view(子控件)的时候

计算机生成了可选文字:8 9 10 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 38 39 41 42 m ViewController.m ) -viewDidLoad 2 targets, iOS SDK 8.3 h AppDelegate.h m AppDelegate.m h ViewController.h ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files h YCCustomView.h m YCCustomView.m YCCustomView.xib Products #import "ViewController. h" #import "YCCustomView. h" @interface V iewCont ro I ler @end @implementation ViewCont ro Ile r void) viewDidLoad super viewDidLoad *customView: LYCCustomView customView) ; YCCustomView // frame, customView, f rame self. view. bounds • customView, backg roundColor UIColor redColor self. view ; / / iew (customView removeFromSuperview void)didReceiveMemoryWarning rec reated . super didReceiveMemoryWa rning // Dispose of any resources that can be @end

 

 

  • 重写相应的方法,监听其执行顺序

计算机生成了可选文字:NSBundle mainBundle) loadNibNamed owner: nil options 2 targets, iOS SDK 8.3 ) -willMoveToSuperview: YCCustomView.m 16 17 18 19 20 21 22 retu rn #pragma mark - void) didAddSubview: ( UlView subview h h AppDelegate.h AppDelegate.m ViewController.h ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files YCCustomView.h YCCustomView.m us om lew.Xl NSLog NSLog Lsubview n; class func_) ; 23 #pragma marl — void)willMoveToSuperview: ( newSuperview UlView 24 25 26 27 , newSuperview) ; NSLog func_) ; NSLog Products 28 mark - void)didMoveToSuperview 29 30 31 func_) ; NSLog 32 #pragma mark — void)willMoveToWindow: (UIWindow newWindow 33 34 35 36 , newWindow) ; NSLog func_) ; NSLog 37 #pragma mark — 38 - ( void)didMoveToWindow func_) ; NSLog 39 #pragma mark — 41 void ) willRemoveSubview: ( *)subview UlView 42 43 45 46 47 48 49 50 NSLog NSLog #pragma mark — void)dealloc Lsubview n; class func_) ; NSLog I

 

 

计算机生成了可选文字:38 - ( 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 2ø15-ø6-27 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 39 YCCustomView.m ) -willMoveToSuperview: iOS Simulator - iPhone 6 - iPh... 6:33 PM p aylll instancetype)customView Carrier return NSBundle mainBundle) loadNibNamed #pragma mark — void) didAddSubview: ( *)subview UlView 2 targets, iOS SDK 8.3 h h h AppDelegate.h AppDelegate.m ViewController.h ViewController.m Main.storyboard Images.xcassets LaunchScreen.xib Supporting Files YCCustomView.h YCCustomView.m YCCustomView.xib NSLog NSLog Lsubview n; class func_) ; #pragma marl — void ) willMoveToSupe rview: ( newSuperview UlView NSLog NSLog , newSuperview) ; func_) ; Products #pragma mark — void)didMoveToSuperview func_) ; NSLog #pragma mark — %lJUIWindow-EWißE void)willMoveToWindow: (UIWindow newWindow NSLog NSLog #pragma mark — , newWindow) ; func_) ; void)didMoveToWindow NSLog 11 func_) ; 18:29:52.873 18:29:52.873 18:29:52.873 18:29:52.873 18:29:52.873 18:29:52.873 18:29:52.879 18:29:52.879 18:29: 52.88ø • 126943ø1 YCCustomV • 126943ø1 UlLabe1 •126943ø1 - (YCCustomView • 126943ø1 UIButton •126943ø1 - (YCCustomView • 126943ø1 UISwitch •126943ø1 - (YCCustomView • 126943ø1 UlTextFie1d •126943øl - (YCCustomView willRemoveSubview: I willRemoveSubview: I willRemoveSubview: I willRemoveSubview: I

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值