【问题标题】:subview not resized with addSubview子视图未使用 addSubview 调整大小
【发布时间】:2014-08-09 09:53:33
【问题描述】:

我有一个带有子视图的 testViewController。自动布局已关闭。在 InterfaceBuilder 中,我设置了 Autosizing 属性。

我从我的 MatchViewContoller 中调用这个 viewController:

-(void)showTutorial
{
    self.testViewController = [[TestViewController alloc] init];
    [self.view addSubview:self.testViewController.view];
}

在testViewController中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:.6];
}

视图显示正确,但是当我在 iPhone 3.5 英寸模拟器中打开视图时,子视图未调整大小。所以现在底部是不可见的。

我已经用[self.navigationController pushViewController:testViewController animated:NO]; 尝试过了,然后它工作正常......子视图的子视图被完美地调整了大小。

但是当我使用[self.view addSubview:self.testViewController.view]; 时,子视图没有调整大小......

我已尝试在 testViewController 和 MatchViewController 上打开和关闭“自动调整子视图”的所有组合...但没有任何效果...

我在这里缺少什么?使用 addSubview 方法添加时调整大小不起作用吗?

【问题讨论】:

  • 我不确定,但试试这个,[self.testViewController.view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin]; 可能对你有帮助
  • 在添加语句后尝试添加[self.view setNeedsLayout]

标签: ios objective-c resize addsubview


【解决方案1】:

问题是您将 testViewController 的视图添加为子视图,但您没有在 viewController 层次结构中添加控制器,因此不会调用诸如 viewDidLoad 之类的生命周期调用。

试试这个:

-(void)showTutorial
{
    self.testViewController = [[TestViewController alloc] init];
    [self addChildViewController:self.testViewController];
    [self.testViewController didMoveToParentViewController:self];
    [self.view addSubview:self.testViewController.view];
}

【讨论】:

  • 不,这并不能解决问题。我还调试了,在我自己的解决方案中, ViewDidLoad 被称为...
猜你喜欢
  • 2012-08-28
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 2011-07-17
相关资源
最近更新 更多