问题:设置完约束之后是无法立刻获取frame的
解决:layoutSubviews 视图布局完成之后,此时试图的大小都已经获取到了,在设置
代码:如下
- (void)layoutSubviews{
[super layoutSubviews];
self.showEmitterView.layer.cornerRadius = self.showEmitterView.width/2;
self.showCountLabel.layer.cornerRadius = self.showCountLabel.width/2;
self.showEmitterView.risingY = self.showCountLabel.height/2;
}
那么问题来了~如果再controller中呢~如何获取view的frame
解决:看uikit中uiviewcontroller中这一段代码~(看注释)
// Called just before the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0);
意思就是:重写viewDidLayoutSubviews吧,这个方法能获取到~
- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
self.showEmitterView.layer.cornerRadius = self.showEmitterView.width/2;
self.showCountLabel.layer.cornerRadius = self.showCountLabel.width/2;
self.showEmitterView.risingY = self.showCountLabel.height/2;
}
网友评论