UIImagePickerController 在关闭时复制内容
UIImagePickerController duplicates content on close
当我调出一个 UIImagePickerController 然后关闭它时,它会复制我模态 window 中的内容。以下是前后对比图:
这是显示图像选择器的代码:
-(void) choosePhotos
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];
[imagePicker setAllowsEditing:YES];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagePicker animated:YES completion:nil];
}
这是我的其余代码(如果需要):
-(id) init
{
self = [super init];
if (self)
{
[self.navigationItem setTitle:@"Deposit"];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
[self.navigationItem setLeftBarButtonItem:closeButton];
toItems = @[@"Account...5544", @"Account...5567"];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:recognizer];
}
return self;
}
-(void) hideKeyboard
{
for (UITextField *field in [scrollView subviews])
{
[field resignFirstResponder];
}
}
-(void) cancel
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [toItems count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [toItems objectAtIndex:row];
}
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.view setBackgroundColor:[UIColor whiteColor]];
UILabel *toLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 50, 100)];
[toLabel setText:@"To:"];
toPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(130, -30, 220, 100)];
[toPicker setDataSource:self];
[toPicker setDelegate:self];
UILabel *amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 70, 100)];
amountLabel.lineBreakMode = NSLineBreakByWordWrapping;
amountLabel.numberOfLines = 0;
[amountLabel setText:@"Check Amount:"];
UITextField *amountField = [[UITextField alloc] initWithFrame:CGRectMake(130, 100, 270, 100)];
[amountField setPlaceholder:@"Enter Amount"];
[amountField setReturnKeyType:UIReturnKeyDone];
[amountField setKeyboardType:UIKeyboardTypeDecimalPad];
UILabel *imagesLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 70, 100)];
imagesLabel.lineBreakMode = NSLineBreakByWordWrapping;
imagesLabel.numberOfLines = 0;
[imagesLabel setText:@"Check Images:"];
UIButton *imagesButton = [[UIButton alloc] initWithFrame:CGRectMake(120, 200, 244, 99)];
[imagesButton setBackgroundImage:[UIImage imageNamed:@"photos.png"] forState:UIControlStateNormal];
[imagesButton addTarget:self action:@selector(choosePhotos) forControlEvents:UIControlEventTouchUpInside];
CGRect bounds = [[UIScreen mainScreen] bounds];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
[scrollView setAlwaysBounceVertical:YES];
[scrollView setShowsVerticalScrollIndicator:YES];
[scrollView addSubview:toLabel];
[scrollView addSubview:toPicker];
[scrollView addSubview:amountLabel];
[scrollView addSubview:amountField];
[scrollView addSubview:imagesLabel];
[scrollView addSubview:imagesButton];
[self.view addSubview:scrollView];
}
每次调用 viewWillAppear
时,您的 UI 元素都会添加到您的视图中。当您的图像选择器关闭并 returns 到您的视图时调用,因此它们被复制。在再次创建它们之前检查您的 UI 元素是否已经存在,或者在 viewDidLoad
方法中进行 UI 设置,该方法只会 运行 一次。您或许可以试试这个,使用 BOOL 属性 来跟踪:
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
if (!self.alreadyAppeared) {
self.alreadyAppeared = YES;
// Create labels and buttons
}
}
我建议您使用 viewDidLoad
作为创建和添加视图的地方:
- (void)viewDidLoad {
[super viewDidLoad];
//init and add your views here
//example view
self.someLabel = [[UILabel alloc] init];
self.someLabel.text = @"someExampleText";
[self.view addSubview:self.someLabel];
}
并且 viewWillAppear
或 viewDidLayoutSubviews
作为配置它们大小的地方(我更喜欢 viewDidLayoutSubviews
所以我会用它作为例子):
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.someLabel.frame = CGRectMake(kMargin,kMargin,kLabelWidth,kLabelHeight);
}
当然,为了做到这一点,您需要通过在界面中为它们创建 属性 来引用您希望以这种方式配置的所有视图:
@interface YourViewController ()
@property (nonatomic, strong) UILabel *someLabel;
@end;
static CGFloat const kMargin = 20.0f;
static CGFloat const kLabelHeight = 30.0f;
static CGFloat const kLabelWidth = 100.0f;
此外,建议您避免使用硬编码值作为它们的大小(像 CGRectMake(20,20,100,70)
那样做,但这并不是完全错误的。
不使用硬编码值并不意味着要自己设置它们,它只是意味着使它们的值更具可读性(并且在大多数情况下是动态的)。
在我的示例中,我创建了 kMargin
、kLabelHeight
和 kLabelWidth
,这意味着任何查看此代码的人都会理解它们的意思,他们会知道在需要时要更改什么,以及这些值可以用在其他地方。
例如,您可以有 4 个标签,为了让它们都遵循相同的布局规则,它们都将使用 origin.x
上的 kMargin
值。
您也可以实现一个动态值,而不是使用宽度的静态值,如下所示:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat labelWidth = self.view.bounds.size.width - (kMargin * 2);
self.someLabel.frame = CGRectMake(kMargin,kMargin,labelWidth,kLabelHeight);
}
我在这里所做的是使我的标签具有与我的超级视图相同的宽度,但我让它考虑了左右边距(通过获取总视图宽度并减少两倍边距值) .
由于我们在 viewDidLayoutSubviews
方法上执行此操作,每当父视图更改其大小(例如,方向更改)时都会调用该方法,这将确保您的 UILabel 可以在任何大小的视图和方向上显示没有额外的代码来处理 'specific cases'.
当我调出一个 UIImagePickerController 然后关闭它时,它会复制我模态 window 中的内容。以下是前后对比图:
这是显示图像选择器的代码:
-(void) choosePhotos
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];
[imagePicker setAllowsEditing:YES];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagePicker animated:YES completion:nil];
}
这是我的其余代码(如果需要):
-(id) init
{
self = [super init];
if (self)
{
[self.navigationItem setTitle:@"Deposit"];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
[self.navigationItem setLeftBarButtonItem:closeButton];
toItems = @[@"Account...5544", @"Account...5567"];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:recognizer];
}
return self;
}
-(void) hideKeyboard
{
for (UITextField *field in [scrollView subviews])
{
[field resignFirstResponder];
}
}
-(void) cancel
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [toItems count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [toItems objectAtIndex:row];
}
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.view setBackgroundColor:[UIColor whiteColor]];
UILabel *toLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 50, 100)];
[toLabel setText:@"To:"];
toPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(130, -30, 220, 100)];
[toPicker setDataSource:self];
[toPicker setDelegate:self];
UILabel *amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 70, 100)];
amountLabel.lineBreakMode = NSLineBreakByWordWrapping;
amountLabel.numberOfLines = 0;
[amountLabel setText:@"Check Amount:"];
UITextField *amountField = [[UITextField alloc] initWithFrame:CGRectMake(130, 100, 270, 100)];
[amountField setPlaceholder:@"Enter Amount"];
[amountField setReturnKeyType:UIReturnKeyDone];
[amountField setKeyboardType:UIKeyboardTypeDecimalPad];
UILabel *imagesLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 70, 100)];
imagesLabel.lineBreakMode = NSLineBreakByWordWrapping;
imagesLabel.numberOfLines = 0;
[imagesLabel setText:@"Check Images:"];
UIButton *imagesButton = [[UIButton alloc] initWithFrame:CGRectMake(120, 200, 244, 99)];
[imagesButton setBackgroundImage:[UIImage imageNamed:@"photos.png"] forState:UIControlStateNormal];
[imagesButton addTarget:self action:@selector(choosePhotos) forControlEvents:UIControlEventTouchUpInside];
CGRect bounds = [[UIScreen mainScreen] bounds];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
[scrollView setAlwaysBounceVertical:YES];
[scrollView setShowsVerticalScrollIndicator:YES];
[scrollView addSubview:toLabel];
[scrollView addSubview:toPicker];
[scrollView addSubview:amountLabel];
[scrollView addSubview:amountField];
[scrollView addSubview:imagesLabel];
[scrollView addSubview:imagesButton];
[self.view addSubview:scrollView];
}
每次调用 viewWillAppear
时,您的 UI 元素都会添加到您的视图中。当您的图像选择器关闭并 returns 到您的视图时调用,因此它们被复制。在再次创建它们之前检查您的 UI 元素是否已经存在,或者在 viewDidLoad
方法中进行 UI 设置,该方法只会 运行 一次。您或许可以试试这个,使用 BOOL 属性 来跟踪:
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
if (!self.alreadyAppeared) {
self.alreadyAppeared = YES;
// Create labels and buttons
}
}
我建议您使用 viewDidLoad
作为创建和添加视图的地方:
- (void)viewDidLoad {
[super viewDidLoad];
//init and add your views here
//example view
self.someLabel = [[UILabel alloc] init];
self.someLabel.text = @"someExampleText";
[self.view addSubview:self.someLabel];
}
并且 viewWillAppear
或 viewDidLayoutSubviews
作为配置它们大小的地方(我更喜欢 viewDidLayoutSubviews
所以我会用它作为例子):
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.someLabel.frame = CGRectMake(kMargin,kMargin,kLabelWidth,kLabelHeight);
}
当然,为了做到这一点,您需要通过在界面中为它们创建 属性 来引用您希望以这种方式配置的所有视图:
@interface YourViewController ()
@property (nonatomic, strong) UILabel *someLabel;
@end;
static CGFloat const kMargin = 20.0f;
static CGFloat const kLabelHeight = 30.0f;
static CGFloat const kLabelWidth = 100.0f;
此外,建议您避免使用硬编码值作为它们的大小(像 CGRectMake(20,20,100,70)
那样做,但这并不是完全错误的。
不使用硬编码值并不意味着要自己设置它们,它只是意味着使它们的值更具可读性(并且在大多数情况下是动态的)。
在我的示例中,我创建了 kMargin
、kLabelHeight
和 kLabelWidth
,这意味着任何查看此代码的人都会理解它们的意思,他们会知道在需要时要更改什么,以及这些值可以用在其他地方。
例如,您可以有 4 个标签,为了让它们都遵循相同的布局规则,它们都将使用 origin.x
上的 kMargin
值。
您也可以实现一个动态值,而不是使用宽度的静态值,如下所示:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat labelWidth = self.view.bounds.size.width - (kMargin * 2);
self.someLabel.frame = CGRectMake(kMargin,kMargin,labelWidth,kLabelHeight);
}
我在这里所做的是使我的标签具有与我的超级视图相同的宽度,但我让它考虑了左右边距(通过获取总视图宽度并减少两倍边距值) .
由于我们在 viewDidLayoutSubviews
方法上执行此操作,每当父视图更改其大小(例如,方向更改)时都会调用该方法,这将确保您的 UILabel 可以在任何大小的视图和方向上显示没有额外的代码来处理 'specific cases'.