当选择器视图出现时如何与键盘出现类似地滚动屏幕?

发布于 2024-12-12 08:34:08 字数 173 浏览 1 评论 0原文

我有一个如图所示的屏幕。现在我希望当用户单击提交的文本时会出现一个选择器视图。所以我希望当选择器视图出现时,视图将在键盘出现时类似地自动滚动。 screen

当选择器视图出现时怎么办?

提前致谢...

I have a screen which i shown in image. Now i want that when user click on text filed then a picker view appears. So i want that when picker view appear then view will scroll automatically similarly when keyboard appear. screen

How do that when picker view appears?

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢望 2024-12-19 08:34:08

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

https://github.com/reednj/TDSemiModal

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

You should have a look at this great project on gitHub:

https://github.com/reednj/TDSemiModal

It provides a class for making a picker appear like the keyboard, with moving the connected field into view.

美人迟暮 2024-12-19 08:34:08

我假设您希望您的应用程序是纵向的,因为如果它是横向的,您可以进行更改。将 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 设置为 480 坐标,因此它位于视图下方。

编辑:如果您希望将 UIPickerView 连接起来并充当 UITextField的键盘>UITextView,您始终可以将其连接到 UITextField.inputView 属性,这样也可以工作。

希望有帮助!

I will assume that you want your app to be in portrait, as you can make the changes if it is landscape. Add a UIPickerView to your nib and hook it up to an IBOutlet. Then, make a button with an IBAction method that animates the UIPickerView. Try in the .h file:

@interface MyViewController: UIViewController {
  BOOL                    _pickerIsVisible;
  IBOutlet UIPickerView * _picker;
}

- (IBAction)buttonMethod:(UIButton *)aButton;

and in your .m file:

- (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.
            }];
  }
}

Make sure you set your UIPickerView in the nib to have a y coordinate of 480, so it is below the view.

Edit: If you want the UIPickerView to be hooked up and act like the keyboard of a UITextField or UITextView, you could always hook it up to the .inputView property of the UITextField, and that would work as well.

Hope that Helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文