xcode 无法更改 UIView 框架大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10382358/
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
Cannot change UIView frame size
提问by michaela
I have a custom uiview that i am trying to update the frame size. I do it like this in a method once it is clicked. The method is called, and the frame value changes, however on the screen nothing happens!
我有一个自定义 uiview,我正在尝试更新框架大小。单击后,我会在一种方法中这样做。该方法被调用,帧值发生变化,但在屏幕上没有任何反应!
if (!touched) {
GameBox *box = (GameBox *)[self.view viewWithTag:40];
[box setFrame:CGRectMake(20, 20, 100, 73)];
NSLog(@"%f", box.frame.origin.x);
markButton.enabled = NO;
guessButton.enabled = NO;
[self.view reloadInputViews];
NSLog(@"The action bar should now be hidden");
}
else if (touched) {
guessButton.enabled = YES;
markButton.enabled = YES;
[self.view reloadInputViews];
NSLog(@"The action bar should now be visible");
}
回答by Anh Nguyen
I guess you have hooked self.view to UIView in Interface Builder
.
我猜你已经在Interface Builder
.
In order to change UIView frame, in Interface Builder
go to File Inspector
, and uncheck Use Autolayout
, then in the code change the frame.
要更改 UIView 框架,请在Interface Builder
go toFile Inspector
和 uncheck 中Use Autolayout
,然后在代码中更改框架。
Hope this helps.
希望这可以帮助。
回答by Michael Peterson
You need to set adjust the frame in the viewDidAppear (not viewDidLoad).
您需要在viewDidAppear(不是viewDidLoad)中设置调整框架。
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGRect frame = self.tableView.frame;
frame.origin.y += 100.0;
frame.size.height -= 100.0;
self.tableView.frame = frame;
}
Apparently this has something to do with table views within navigation controllers.
显然,这与导航控制器中的表格视图有关。
回答by Deniz
It is because of Auto Layout, however you could do it after Auto Layout done its work or change constraints of it. If you want to do it after auto layout add following.
这是因为自动布局,但是您可以在自动布局完成其工作或更改其约束后进行。如果您想在自动布局后添加以下内容。
- (void)viewDidAppear:(BOOL)animated {
dispatch_async(dispatch_get_main_queue(), ^{
box.frame = CGRectMake(20, 20, 100, 73)];
});
回答by Abhishek Singh
[self.view reloadInputViews]
is generally used for FirstResponders. I Don't have much of an idea about your customview, but I think for just settings the frame you can use:
[self.view reloadInputViews]
一般用于FirstResponders。我对您的自定义视图没有太多想法,但我认为仅设置您可以使用的框架:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
viewFrame.frame=frame;//Frame Defined by you
[UIView commitAnimations];
can give better solution if i get a look at your custom view.
如果我查看您的自定义视图,可以提供更好的解决方案。