【问题标题】:Cannot change UIView frame size无法更改 UIView 框架大小
【发布时间】:2012-04-30 10:44:52
【问题描述】:

我有一个自定义 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");
    }

【问题讨论】:

  • 请问你想做什么..这样我才能理解..
  • 我想:a) 调整 uisubview 框的 cgrect 框架的大小,b) 禁用/重新启用猜测按钮和标记按钮

标签: iphone xcode uiview frame cgrect


【解决方案1】:

我猜你已经在Interface Builder 中将 self.view 连接到 UIView。

为了更改 UIView 框架,在 Interface Builder 中转到 File Inspector,并取消选中 Use Autolayout,然后在代码中更改框架。

希望这会有所帮助。

【讨论】:

  • 不敢相信我花了 3 个小时调试...谢谢!
  • 这可行,但会影响对整个文档使用自动布局的能力...
  • 你可以简单地设置 translatesAutoresizingMaskIntoConstraints = YES;在 uiView 元素上并仍然保留您的自动布局。这会将元素重绘到新框架。
【解决方案2】:

您需要在 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;
}

显然这与导航控制器中的表格视图有关。

【讨论】:

    【解决方案3】:

    这是因为 Auto Layout,但是您可以在 Auto Layout 完成它的工作或更改它的约束后执行它。如果您想在自动布局后添加以下内容。

    - (void)viewDidAppear:(BOOL)animated {
        dispatch_async(dispatch_get_main_queue(), ^{
            box.frame = CGRectMake(20, 20, 100, 73)];
    });
    

    【讨论】:

    • 它帮助了我,谢谢,但你能告诉我为什么你把它放在调度块@Deniz 中
    • 似乎我希望在当前重绘周期后更改帧大小,在 viewDidAppear 方法完成执行后的一段时间。但我现在不确定。
    【解决方案4】:

    [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]; 
    

    如果我看看您的自定义视图,可以提供更好的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 2014-04-26
      • 2017-10-16
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多