递归添加self.view到self.view?

时间:2009-11-05 12:08:29

标签: iphone ios cocoa-touch uiview subview

我需要在主动主视图中添加一个新的子视图。此子视图应该包含放在主视图中的所有内容。子视图有一个frame = view.frame / 2。 我尝试实现一些解决方案(下面),但没有结果((

首先关闭

 CGRect frame = CGRectMake(100, 140, 250, 250);
 UIView *view = [self.view copy];
 view.frame = frame;
 [self.view addSubview:view ];
 [view release];

然后

 UIView *View = [[[NSBundle mainBundle] loadNibNamed:@"CurrentTemplate" owner:self options:nil] objectAtIndex:0];
 View.frame = CGRectMake(100, 140, 250, 250);
 [self.view addSubview:view ];
 [view release];

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您想将self.view的所有子视图移动到新视图中,然后将该新视图添加回self.view?

也许这样的事情可能会这样做:

// Create a new empty view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 140, 250, 250)];

// Add each of the children to this new view
NSArray *views = [NSArray arrayWithArray:self.view.subviews];
for (UIView *child in views)
  [view addSubview:child];

// Add the new view as the child of self.view
[self.view addsubview:view];
[view release];

希望这有帮助,

萨姆