iPhone: Maintain visibility of form inputs – auto-scrolling views【保持表单输入的可视性 - 自动滚动】

9 篇文章 0 订阅
8 篇文章 0 订阅

When you develop forms or any screens with input fields, occasionally the inputs will be obscured by the keyboard when it appears. This is bad usability for the user who now has to input data without being able to see what they have typed! One solution is to slide the whole view so that the field being edited is always visible.

  1. Screen shot 2010-03-25 at 20.21.31Screen shot 2010-03-25 at 20.21.41Screen shot 2010-03-25 at 20.32.47

    - (void) maintainVisibityOfControl:(UIControl *)control offset:(float)offset {
    static const float deviceHeight = 480;
    static const float keyboardHeight = 216;
    static const float gap = 5; //gap between the top of keyboard and the control


    //Find the controls absolute position in the 320*480 window - it could be nested in other views
    CGPoint absolute = [control.superview convertPoint:control.frame.origin toView:nil];


    //If it would be hidden behind the keyboard....
    if (absolute.y > (keyboardHeight + gap)) {
    //Shift the view
    float shiftBy = (deviceHeight - absolute.y) - (deviceHeight - keyboardHeight - gap - offset);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f]; //this is speed of keyboard
    CGAffineTransform slideTransform = CGAffineTransformMakeTranslation(0.0, shiftBy);
    self.transform = slideTransform;
    [UIView commitAnimations];
    }
    }

    - (void) resetViewToIdentityTransform {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f]; //this is speed of keyboard
    CGAffineTransform slideTransform = CGAffineTransformIdentity;
    self.transform = slideTransform;
    [UIView commitAnimations];
    }


    - (void) textFieldDidBeginEditing:(UITextField *)textField {
    [self.view maintainVisibityOfControl:textField offset:0.0f];
    }


    - (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == currentControl) {
    //If the textfield is still the same one, we can reset the view animated
    [self.view resetViewToIdentityTransform];
    }else {
    //However, if the currentControl has changed - that indicates the user has
    //gone into another control - so don't reset view, otherwise animations jump around
    }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值