【问题标题】:How scroll screen when picker view appear similarly when keyboard appear?当键盘出现时选择器视图出现类似情况时如何滚动屏幕?
【发布时间】:2011-12-17 01:51:37
【问题描述】:

我有一个我在图像中显示的屏幕。现在我希望当用户点击文本字段时出现一个选择器视图。所以我希望当键盘出现时,当Picker View出现时,视图将自动滚动。

如何在拾取器视图出现时?

提前谢谢...

【问题讨论】:

  • 您要滚动选择器视图还是滚动视图以使当前问题在选择器上方可见?

标签: iphone uiviewcontroller uiscrollview uipickerview


【解决方案1】:

你应该在 gitHub 上看看这个伟大的项目:

https://github.com/reednj/TDSemiModal

它提供了一个类,用于使选择器看起来像键盘,并将连接的字段移动到视图中。

【讨论】:

    【解决方案2】:

    我假设您希望您的应用是纵向的,因为如果它是横向的,您可以进行更改。将UIPickerView 添加到您的笔尖并将其连接到IBOutlet。然后,使用IBAction 方法制作一个按钮,以动画UIPickerView。在 .h 文件中尝试:

    @interface MyViewController: UIViewController {
      BOOL                    _pickerIsVisible;
      IBOutlet UIPickerView * _picker;
    }
    
    - (IBAction)buttonMethod:(UIButton *)aButton;
    

    在您的 .m 文件中:

    - (void)viewDidLoad; {
      [super viewDidLoad];
      _pickerIsVisible = NO;
    }
    
    - (IBAction)buttonMethod:(UIButton *)aButton; {
      if(_pickerIsVisible){
        _pickerIsVisible = NO;
        [UIView animateWithDuration:2.0f
                animations:^{ 
                    CGPoint point = _picker.frame.origin;
                    point.y += 216; // The height of the picker.
                    _picker.frame.origin = point;
                } 
                completion:^(BOOL finished){
                  // Do something here if you want.
                }];
      }
      else{
        _pickerIsVisible = YES;
        [UIView animateWithDuration:2.0f
                animations:^{ 
                    CGPoint point = _picker.frame.origin;
                    point.y -= 216; // The height of the picker.
                    _picker.frame.origin = point;
                } 
                completion:^(BOOL finished){
                  // Do something here if you want.
                }];
      }
    }
    

    确保在笔尖中设置UIPickerView 的 y 坐标为 480,因此它位于视图下方。

    编辑:如果您希望UIPickerView 被连接起来并像UITextFieldUITextView 的键盘一样,您可以随时连接它直到UITextField.inputView 属性,这也可以。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 2012-02-01
      • 2012-06-29
      • 2012-02-18
      相关资源
      最近更新 更多