点击 UITextField 使 UIScrollView 变小

Posted

技术标签:

【中文标题】点击 UITextField 使 UIScrollView 变小【英文标题】:Clicking on UITextField makes UIScrollView Smaller 【发布时间】:2013-05-30 11:22:11 【问题描述】:

我有一个UIScrollView,在UIScrollView 中有UITextFeild,在那个UIScrollView 中。我的问题是,点击UITextfFeild 后,UIScrollView 变得比我设置的要小。

我设置了我的UIScrollView:

-(void) viewDidAppear:(BOOL)animated 
myScrollView.contentSize = CGSizeMake (320,800);

当我点击UITextfeild 时,UIScrollView 从 800 变为更小的约 500?

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController
AVAudioPlayer *player;

@synthesize myScrollView;
@synthesize questionLabel, actionBarLabel, questionPlaceholder, commentsLabel;
@synthesize imageView, firstNameTextField,commentsTextField;

- (void)viewDidLoad

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myScrollView.maximumZoomScale = 0.0;
myScrollView.minimumZoomScale = 0.0;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
[myScrollView addSubview:imageView];
[myScrollView setScrollEnabled:YES];
[myScrollView addSubview:questionLabel];
[myScrollView addSubview:firstNameTextField];
[myScrollView addSubview:commentsTextField];
[myScrollView addSubview:actionBarLabel];
[myScrollView addSubview:questionPlaceholder];
[myScrollView addSubview:commentsLabel];

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setFrame:CGRectMake(10.0f, 600.0f, 300.0f, 42.0f)];
[btn1 setTitle:[NSString stringWithFormat:@"Choose an Option"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
[super viewDidLoad];
[self.myScrollView addSubview:btn1];

[self performSelector:@selector(showGradientForQuestions1) withObject:nil afterDelay:0];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *imageData = [defaults dataForKey:@"image"];
UIImage *contactImage = [UIImage imageWithData:imageData];

NSString *frontName = [defaults objectForKey:@"firstname"];
NSString *comments = [defaults objectForKey:@"commentsText"];

// Update the UI elements with the saved data
imageView.image = contactImage;
firstNameTextField.text = frontName;
commentsTextField.text = comments;
firstNameTextField.delegate = self;
commentsTextField.delegate = self;


- (void)didReceiveMemoryWarning

[super didReceiveMemoryWarning];


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

[self dismissViewControllerAnimated:YES completion:nil];


- (IBAction) save:(id)sender

[firstNameTextField resignFirstResponder];
[commentsTextField resignFirstResponder];

// Create instace of NSData
UIImage *contactImage = imageView.image;
NSData *imageData = UIImageJPEGRepresentation(contactImage, 100);
NSString *frontName = [firstNameTextField text];
NSString *comments = [commentsTextField text];

// Store the data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:imageData forKey:@"image"];
[defaults setObject:frontName forKey:@"firstname"];
[defaults setObject:comments forKey:@"commentsText"];

[defaults synchronize];

NSLog(@"Data saved");

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Data Saved!" message:@" Successfully saved to cache" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];



- (void)viewDidUnload

imageView = nil;
firstNameTextField = nil;
commentsTextField = nil;
myScrollView = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;


-(BOOL) textFieldShouldReturn:(UITextField *)textField

[firstNameTextField resignFirstResponder];
[commentsTextField resignFirstResponder];
[imageView resignFirstResponder];
[myScrollView resignFirstResponder];
[self.navigationController setNavigationBarHidden:NO];

return YES;


-(void) viewDidAppear:(BOOL)animated 
myScrollView.contentSize = CGSizeMake (320,800);

@end

【问题讨论】:

为什么要让滚动视图比视图的实际内容高很多? 因为我最终需要填写该空间。目前,如果您看到我在 600 处放置了一个按钮,但它隐藏在图像中,并且在编辑文本字段后我无法滚动到 600 处的按钮。 你确定你的 scoroll 的 contentSize 变小还是滚动视图向上滚动? 是的 UIScrollview 向上滚动,但是当我想再次向下滚动到按钮时,它不会让我。 你在文本字段委托方法中写了什么? 【参考方案1】:

可能是因为您的滚动视图剪辑到边界?

myScrollView.clipsToBounds = YES;

【讨论】:

不抱歉,试图将剪辑注释掉。没有解决同样的问题。【参考方案2】:

当您单击 textview 或 textfield 时,键盘会出现并隐藏部分屏幕,并且您将无法滚动视图的其余部分,直到键盘消失。尝试在textFieldDidBeginEditing 函数中增加滚动视图的高度,并在textFieldDidEndEditing 函数中减少相同的量。

scrollView.contentSize=CGSizeMake(scrollView.contentSize.width,scrollView.contentSize.height+KeyBoardSizePixel);

【讨论】:

以上是关于点击 UITextField 使 UIScrollView 变小的主要内容,如果未能解决你的问题,请参考以下文章

如何使键盘 UITextField 中的数字视图成为默认值?

UIScrollView 使 UITextField 停止调用 UIPicker

如果将键盘设置为特定语言并将继续作为返回键,则 UITextField 会使应用程序崩溃

iOS - 当文本字段在子视图内时,键盘上的 UIScroll 弹出到文本字段

不要在 UITextField 返回时关闭键盘

iOS UITextField resignFirstResponder 问题