UIViewController移除子视图

3
我将一个视图作为我的UIViewController的子视图添加,方法如下:

// into my ViewController:
UIImageView *imView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img.jpg"]];
imView.frame = CGRectMake(2, 46, 1020, 720);
[self.view addSubview:imView];

现在,我希望用另一个按钮将imView从子视图链中移除,我该怎么做呢?谢谢。
1个回答

5
你可以调用这个方法,它属于 UIView(UIImageView 从 UIView 继承)。

UIView - (void)removeFromSuperview

(注:该方法为UIView的 removeFromSuperView 方法)

谢谢!我尝试过了,但是UIImageView仍然在ViewController中... 我查看了您提供的链接,上面写着“在显示时不要调用此方法。”... 但是我需要在显示时移除它... - ghiboz
1
你确定是 “when displaying” 而不是 “when it is on display” 吗?你可以看一下这个问题,它可能能回答你的问题: https://dev59.com/REjSa4cB1Zd3GeqPFnqT - vodkhang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接