关于self.view.window与viewDidLoad、viewWillAppear、viewDidAppear
在进入一个界面(UIViewController),如果要在进入的时候使用self.view.window,那么必须在将使用代码放在viewDidAppear方法中,而viewDidLoad、viewWillAppear中self.view.window.frame为0{{0,0},{0,0}}。
- (void)viewDidLoad { NSLog(@"viewDidLoad"); [super viewDidLoad]; } -(void)viewWillAppear:(BOOL)animated{ NSLog(@"viewWillAppear"); [super viewWillAppear:animated]; } -(void)viewDidAppear:(BOOL)animated{ NSLog(@"viewDidAppear"); [super viewDidAppear:animated]; [self _doTest01]; } - (void)_doTest01 { NSLog(@"_doTest01"); //UIView UIView* vwFullScreenView = [[UIView alloc]init]; vwFullScreenView.backgroundColor=[UIColor yellowColor]; vwFullScreenView.frame=self.view.window.frame; NSLog(@"%@",NSStringFromCGRect(vwFullScreenView.frame)); [self.view.window addSubview:vwFullScreenView]; //UILabel UILabel* lblTitle = [[UILabel alloc]init]; lblTitle.text=@"显示了@_@"; lblTitle.frame=CGRectMake(0, 0, 200, 40); lblTitle.textAlignment=NSTextAlignmentCenter; lblTitle.backgroundColor=[UIColor whiteColor]; lblTitle.center=vwFullScreenView.center; [vwFullScreenView addSubview:lblTitle]; }
输出:
2015-11-01 09:21:27.031 demo010[7638:261914] viewDidLoad
2015-11-01 09:21:27.032 demo010[7638:261914] viewWillAppear
2015-11-01 09:21:27.038 demo010[7638:261914] viewDidAppear
2015-11-01 09:21:27.038 demo010[7638:261914] _doTest01
2015-11-01 09:21:27.039 demo010[7638:261914] {{0, 0}, {320, 568}}
如果将[self _doTest01];代码放到viewDidLoad 或 viewWillAppear 方法中,那么输出结果如下:
2015-11-01 09:35:00.054 demo010[7762:271018] viewDidLoad
2015-11-01 09:35:00.054 demo010[7762:271018] viewWillAppear
2015-11-01 09:35:00.054 demo010[7762:271018] _doTest01
2015-11-01 09:35:00.054 demo010[7762:271018] {{0, 0}, {0, 0}}
2015-11-01 09:35:00.061 demo010[7762:271018] viewDidAppear
可以看到self.view.window的frame为 {{0, 0}, {0, 0}},说明此时self.view.window还没显示出来。
相关推荐
self.view.insertSubview(self.leftViewController.view, atIndex:0) } } if self.centerViewController != nil{ if (self.centerViewController.view.superview == nil){ self.addChildViewController...
因此,正确的做法是在`viewDidLoad`或更晚的生命周期方法中设置`self.view`的属性,如`viewWillAppear:`或`viewDidAppear:`。这样可以确保视图的配置在视图被加载且准备显示时进行,避免生命周期错乱。 总结来说,...
_cuiPickerView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 200); //这一步很重要 _cuiPickerView.myTextField = _textField; _cuiPickerView.delegate = self; _...
self.scrollView.contentSize = CGSize(width: self.view.bounds.size.width * 3, height: hBounds/2) //Putting a subview in the navigationbar to hold the titles and page dots navbarView = UIView() ...
self.view1.layer.transform = CATransform3DMakeRotation(CGFloat.pi, 0, 1, 0) self.view1.alpha = 0 // 视图2从背面翻转到正面 self.view2.layer.transform = CATransform3DIdentity self.view2.alpha = 1...
ios7 demo下载 searchbar与状态栏重叠已修复。 关键代码: -(void)viewDidLoad{ [super viewDidLoad]; // self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];//去除多余行 //设置table...
`viewDidLoad`只是生命周期中的一个阶段,你还可以在其他方法中,如`viewWillAppear:`、`viewDidAppear:`、`viewWillDisappear:`和`viewDidDisappear:`等,根据需求进行额外的视图配置和逻辑处理。 总之,通过编程...
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:[[WKWebViewConfiguration alloc] init]]; self.webView.navigationDelegate = self; // 注册消息处理器 ...
self.view = [[GLKView alloc] initWithFrame:self.view.bounds context:self.context]; self.view.drawableDepthFormat = GLKViewDrawableDepthFormat24; self.view.drawableStencilFormat = ...
self.map = [[MKMapView alloc] initWithFrame:[self.view bounds]]; self.map.showsUserLocation = YES; // 显示用户位置 self.map.mapType = MKMapTypeSatellite; // 设置为卫星地图模式 [self.view ...
self.indexView = [[MJNIndexView alloc]initWithFrame:self.view.bounds]; self.indexView.dataSource = self; self.indexView.fontColor = [UIColor blueColor]; [self.view addSubview:self.indexView]; ...
self.view.backgroundColor = [UIColor whiteColor]; self.title = @" bsy_Window 演示"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setFrame:CGRectMake([UIScr
### 构建View时可能用到的代理函数调用顺序:viewDidLoad与viewWillAppear #### 概述 在iOS开发过程中,理解视图控制器(ViewController)的生命周期及其相关的代理方法调用顺序至关重要。这些方法帮助开发者正确...
MaskView *maskView = [[MaskView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:maskView]; } ``` 通过这样的方式,我们就实现了简单的遮罩层效果。如果需要动态控制遮罩的显示和消失,可以...
self.label.frame = CGRect(x: 0, y: -textHeight, width: self.view.frame.width, height: 30) self.startScrolling() } } // 在视图加载后启动滚动 override func viewDidLoad() { super.viewDidLoad() ...
NSLayoutConstraint *webViewTop = [NSLayoutConstraint constraintWithItem:webView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop ...
:fishing_pole: 生命周期挂钩 :fishing_pole:关于LifecycleHooks允许将自定义代码注入到视图和视图控制器中,以响应生命周期事件,例如, viewController.on(.viewDidAppear) { animated inprint("View did appear",...
imageView.frame = CGRectMake(i * self.view.bounds.size.width, 0, self.view.bounds.size.width, AD_IMAGE_HEIGHT); [scrollView addSubview:imageView]; } ``` 假设你有一个名为“ad1.jpg”到“adN.jpg”的...
### 关于iOS自动布局知识点详解 #### 一、概述 在iOS开发中,自动布局(Auto Layout)是一种非常重要的布局管理技术。它允许开发者通过设置视图间的约束关系来实现界面元素的位置和大小控制,从而使得应用能够在...
self.view.backgroundColor = [UIColor whiteColor]; [self initpicCount:9]; // Do any additional setup after loading the view, typically from a nib. } /** 初始化 最大可以选照片的张数 @...