gpt4 book ai didi

ios - UIImagePickerController 在关闭时复制内容

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:33 25 4
gpt4 key购买 nike

当我调出一个 UIImagePickerController 然后关闭它时,它会复制我的模态窗口中的内容。以下是前后对比图:

enter image description here

enter image description here

这是显示图像选择器的代码:

-(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];
}

最佳答案

我建议您使用 viewDidLoad 作为创建和添加 View 的地方:

- (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];
}

并且 viewWillAppearviewDidLayoutSubviews 作为配置它们大小的地方(我更喜欢 viewDidLayoutSubviews 所以我会用它作为例子) :

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.someLabel.frame = CGRectMake(kMargin,kMargin,kLabelWidth,kLabelHeight);
}

当然,为了做到这一点,您需要通过在界面中为它们创建一个属性来引用您希望以这种方式配置的所有 View :

@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) 那样做,但这并非完全错误。

不使用硬编码值并不意味着要自己设置它们,它只是意味着使它们的值更具可读性(并且在大多数情况下是动态的)。在我的示例中,我创建了kMarginkLabelHeightkLabelWidth,这意味着任何看过这段代码的人都会明白他们的意思,他们会知道如果需要更改什么,这些值可以在其他地方使用。

例如,您可以有 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);
}

我在这里所做的是使我的标签具有与我的 super View 相同的宽度,但我让它考虑了左右边距(通过采用总 View 宽度并减少两倍边距值)。

由于我们在 viewDidLayoutSubviews 方法上执行此操作,每当父 View 更改其大小(例如,方向更改)时都会调用该方法,这将确保您的 UILabel 可以显示在任何大小的 View 上,并且没有额外代码来处理“特定情况”的方向。

关于ios - UIImagePickerController 在关闭时复制内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31277013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com