2014-04-15 30 views
0

对于用户输入他们的数据转换成文本框中的iPhone应用程序,设置滚动放置文本框在现场编程

我需要的页面滚动时,双方的文本框都被选中供用户输入时键盘上有未打开供用户查看他们输入的内容。

当使用Interface Builder并覆盖滚动视图时,我无法保留或保存页面,因此页面实际上是可滚动的。

我也希望以编程方式做到这一点。像这样的其他解决方案在插入到.m文件中时不起作用。

@property (weak, nonatomic) IBOutlet UIScrollView *topScrollView; 
@synthesize topScrollView; 

[topScrollView setFrame:CGRectMake(320, 0, 320, 65)]; 
[topScrollView setContentSize:CGSizeMake(500, 100)]; 
[topScrollView setBackgroundColor:[UIColor greenColor]]; 
[topScrollView setScrollEnabled:YES]; 
[topScrollView setShowsHorizontalScrollIndicator:YES]; 
[topScrollView setShowsVerticalScrollIndicator:NO]; 
[[self view] addSubview:topScrollView]; 

回答

0

你可以把你的主视图顶部所有文本框一个UIView和键盘设置NSNotifications和周围相应地移动视图。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShowWithNotification:) name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideWithNotification:) name:UIKeyboardWillHideNotification object:nil]; 

#pragma mark NSNotifications 
- (void)keyboardDidShowWithNotification:(NSNotification *)aNotification 
{ 
    const int movementDistance = ([UIScreen mainScreen].bounds.size.height == 568 ? 155 : 180); 
    [UIView animateWithDuration:0.3 
         delay:0 
        options:UIViewAnimationCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState 
       animations:^{ 
        CGPoint adjust = CGPointMake(0, -movementDistance); 
        CGPoint newCenter = CGPointMake(self.loginContainer.center.x+adjust.x, self.loginContainer.center.y+adjust.y); 
        [self.loginContainer setCenter:newCenter]; 
       } 
       completion:nil]; 
} 
- (void)keyboardDidHideWithNotification:(NSNotification *)aNotification 
{ 
    [UIView animateWithDuration:0.3 
         delay:0 
        options:UIViewAnimationCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState 
       animations:^{ 
        self.loginContainer.frame = CGRectMake(0, 0, self.loginContainer.frame.size.width, self.loginContainer.frame.size.height); 
       } 
       completion:nil]; 
} 
+0

你可以展开或添加代码来说明loginContainer是什么或它在这里做什么? – horta

+0

LoginContainer是一个带有我所有子视图的UIView,一旦键盘打开,我将我的logincontainer向上移动,一旦用户完成并键盘关闭,我将logincontainer移回。 – NSDavidObject