在UIScrollView中调整键盘

时间:2014-07-30 07:11:15

标签: ios objective-c keyboard uitextfield

我尝试在UIScrollView中设置UITextField,并使键盘根据需要调整滚动视图进行编辑。我使用的代码不起作用,是否需要对委托代理进行任何其他设置?

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [myScrollView setContentOffset:CGPointMake(0,textField.center.y-140) animated:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [myScrollView setContentOffset:CGPointMake(0,0) animated:YES];

    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [myScrollView setScrollEnabled:YES];
    [myScrollView setContentSize:CGSizeMake(320, 750)];
    UITextField *title = [[UITextField alloc] initWithFrame:CGRectMake(10, 410, 300, 30)];
    title.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
    title.backgroundColor=[UIColor whiteColor];
    title.placeholder = @"Sample Text";
    [myScrollView addSubview:title];
}

1 个答案:

答案 0 :(得分:0)

在viewDidLoad中,您应该为代理添加此内容:

title.delegate = self;

如果看到警告,请在头文件.h add

<UITextFieldDelegate>