在没有滚动视图的简单UIVIew中为UITextField显示键盘时调整界面?

时间:2009-05-26 08:20:39

标签: iphone xcode interface-builder

有没有办法在一个简单的UIView中移动一些文本字段,以便它们不能隐藏在键盘后面。是否可以不使用UIScrollView

1 个答案:

答案 0 :(得分:3)

应该可以调整UITextField所在视图的帧位置。看看这段代码:

http://iphoneincubator.com/blog/tag/uitextfield

- (void)textFieldDidBeginEditing:(UITextField *)textField {  
    [self scrollViewToCenterOfScreen:textField];  
}  

- (void)scrollViewToCenterOfScreen:(UIView *)theView {  
    CGFloat viewCenterY = theView.center.y;  
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];  

    CGFloat availableHeight = applicationFrame.size.height - keyboardBounds.size.height;            // Remove area covered by keyboard  

    CGFloat y = viewCenterY - availableHeight / 2.0;  
    if (y < 0) {  
        y = 0;  
    }  
    scrollView.contentSize = CGSizeMake(applicationFrame.size.width, applicationFrame.size.height + keyboardBounds.size.height);  
    [scrollView setContentOffset:CGPointMake(0, y) animated:YES];  
}