ios - 以编程方式添加重复的 uiview

标签 ios uiview addsubview viewwillappear

我目前正在制作一个照片装饰应用。 *holderView 是用户选择的贴纸。每当我尝试从照片库加载照片或拍摄照片然后加载回此页面时,都会以编程方式添加额外的 *holderView,这是我之前在拍照前选择的贴纸,之后会出现重复的贴纸,这不是我想要的。

我应该如何编写代码才能不让这种情况发生? 非常感谢。

- (void)viewWillAppear:(BOOL)animated {

UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];
...
}

最佳答案

您的问题似乎是您使用的是 viewWillAppear 方法而不是 viewDidLoad。这将导致多个“imageViews”,因为您每次隐藏时都会添加一个新的,然后显示它所在的 viewController。您想要做的是将 imageView 的创建(如果真的只有 1 个)移动到viewDidLoad 方法并使整个类都可以访问该 imageView,然后在 viewWillApear 中只需将 imageView 中的图像更改为新选择的图像。

关于ios - 以编程方式添加重复的 uiview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8759814/

相关文章:

ios - subview 的方向不正确

ios - 为什么按钮只在第二次尝试时出现?

ios - 如何让UIView停止接收触摸事件?

iOS SDK(xcodebuild): Clang error cannot specify -o when generating multiple output files

ios - 用户删除应用程序时触发注销用户的方法 - iPad 应用程序

swift - UIView.animate - 将 .autoreverse 与其他选项一起使用

ios - touchesBegan :withEvent: on UIView in UITableViewCell called with noticable delay

iOS 禁用以编程方式创建的按钮直到功能完成

swift - 在 Uitableviewcontroller 中创建水平滚动区域

ios - UIView 当它是 tableHeaderView 时更新,但当它是 subview 时不更新