ios 为什么 UIView 的框架没有在 ViewDidLayoutSubviews 中更新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22488728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why a frame of a UIView is not updating in ViewDidLayoutSubviews?
提问by nabrugir
I am trying to update the frame of a UIView
which contains buttons and labels inside. I am trying to update it in viewDidLayoutSubviews
(and I also tried in viewDidLoad
, viewWillAppear
, viewDidAppear
..). I want to change the y position (origin.y) of the view.
The NSLogs says my original y position is 334, and after changing, it is 100. However, the position does not change in my view. I have already checked that the view is connected in the storyboard. What am I doing wrong?
我正在尝试更新UIView
其中包含按钮和标签的框架。我想更新了它viewDidLayoutSubviews
(我也试过viewDidLoad
,viewWillAppear
,viewDidAppear
...)。我想更改视图的 y 位置(origin.y)。NSLogs 说我原来的 y 位置是 334,更改后是 100。但是,在我看来,位置没有变化。我已经检查过该视图是否已在故事板中连接。我究竟做错了什么?
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
CGRect theFrame = [self.bottomView frame];
NSLog(@"Y position bottomview: %f", self.bottomView.frame.origin.y);
if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
{
// NSLog(@"iphone5");
}
else{
// NSLog(@"iphone4");
theFrame .origin.y = 100;
}
self.bottomView.frame = theFrame;
NSLog(@"Y position bottomview after changing it: %f", self.bottomView.frame.origin.y);
[self.view layoutIfNeeded];
}
采纳答案by nabrugir
The problem was related with Autolayout. However I couldn't turn it off since I am using autolayout in my project. I solved defining appropriate constraints in the view. Then there is no need to check if it is iPhone 4inch or 3.5inch and change the position of the frame since it automatically adapts to each size.
问题与自动布局有关。但是我无法关闭它,因为我在我的项目中使用了自动布局。我解决了在视图中定义适当约束的问题。这样就不用检查是iPhone 4inch还是3.5inch,也不需要改变边框的位置,因为它会自动适应每种尺寸。
回答by Lars Blumberg
I've had the same problem. Forcing the layouting for your view's superview helped me out:
我遇到了同样的问题。强制布局视图的超级视图帮助了我:
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.bottomView.superview setNeedsLayout];
[self.bottomView.superview layoutIfNeeded];
// Now modify bottomView's frame here
}
In Swift:
在斯威夫特:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
bottomView.superview!.setNeedsLayout()
bottomView.superview!.layoutIfNeeded()
// Now modify bottomView's frame here
}
回答by Alsh compiler
Believe it or not the below code fixed it
信不信由你下面的代码修复了它
override func viewDidLayoutSubviews() {
DispatchQueue.main.async {
// UI changes
}
}
回答by Hlung
The frame setting should work in your code. But if the view has autolayout constraints (which I assume you have), your frame setting won't work. You can only go one way or the other (manual frame setting or autolayout), not both.
框架设置应该适用于您的代码。但是如果视图有自动布局约束(我假设你有),你的框架设置将不起作用。您只能采用一种方式或另一种方式(手动框架设置或自动布局),而不能同时采用两种方式。