当我调出一个UIImagePickerController然后关闭它时,它会复制我的模态窗口中的内容。以下是前后图片:
以下是显示图像选择器的代码:
-(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];
}
答案 0 :(得分:2)
我建议您使用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个标签,为了使它们全部遵循相同的布局规则,所有标签都将使用kMargin
上的origin.x
值。
你也可以,而不是使用宽度的静态值,你可以实现一个动态值,如下所示:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat labelWidth = self.view.bounds.size.width - (kMargin * 2);
self.someLabel.frame = CGRectMake(kMargin,kMargin,labelWidth,kLabelHeight);
}
我在这里做的是让我的标签与我的超级视图具有相同的宽度,但是我将其设置为左边距和右边距(通过获取总视图宽度并减少边距值的两倍)。 / p>
因为我们在viewDidLayoutSubviews
方法上执行此操作,只要superview更改其大小(例如,方向更改)就会调用此方法,这将确保您的UILabel可以在任何大小的视图和方向上显示而无需额外的用于处理“特定案例”的代码。
答案 1 :(得分:1)
每次调用viewWillAppear
时,您的UI元素都会添加到您的视图中。当您的图像选择器解散并返回到您的视图时,会调用此方法,因此它们会被复制。在再次创建UI元素之前检查您的UI元素是否已经存在,或者在viewDidLoad
方法中进行UI设置,该方法仅运行一次。您也许可以尝试这样做,使用BOOL属性来跟踪:
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
if (!self.alreadyAppeared) {
self.alreadyAppeared = YES;
// Create labels and buttons
}
}