显示键盘时向上移动 UIView [英] Moving UIView up when keyboard shown

查看:22
本文介绍了显示键盘时向上移动 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个常见问题,但我没有从我发现的例子中实现这一点.我可能做错了一些简单的事情..(希望如此).我正在尝试将 UIview 向上移动,因为底部隐藏了一个文本字段.

I know this a frequent question but ive had no luck implementing this from examples i have found. i could be doing something simple wrong..(lets hope so). i am trying to move the UIview up as a have a textfield hidden at the bottom.

关于我的应用程序的一点,它有一个标签栏控制器并实现了一个标准的 UIView.当我添加代码以向上移动显示时,什么也没有发生.我是否需要滚动视图才能使其工作,是否与选项卡控制器冲突,还是我做错了什么?谢谢

A little about my app, its got a tab bar controller and implements a standard UIView. When i add the code to move the display up nothing happens. Do i need to have a scroll view for this to work, is it a conflict with tab controller or am i doing something else wrong? thanks

推荐答案

我在 UIView 上写了一个小类别,它可以管理临时滚动内容,而无需将整个内容包装到 UIScrollView 中.我在这里使用的动词滚动"可能并不理想,因为它可能会让您认为涉及到滚动视图,而实际上并没有——我们只是在为 UIView(或 UIView 子类)的位置设置动画.

I wrote a little category on UIView that manages temporarily scrolling things around without needing to wrap the whole thing into a UIScrollView. My use of the verb "scroll" here is perhaps not ideal, because it might make you think there's a scroll view involved, and there's not--we're just animating the position of a UIView (or UIView subclass).

其中嵌入了许多适合我的形式和布局但可能不适合您的神奇数字,因此我鼓励对其进行调整以满足您的特定需求.

There are a bunch of magic numbers embedded in this that are appropriate to my form and layout that might not be appropriate to yours, so I encourage tweaking this to fit your specific needs.

UIView+FormScroll.h:

UIView+FormScroll.h:

#import <Foundation/Foundation.h>

@interface UIView (FormScroll) 

-(void)scrollToY:(float)y;
-(void)scrollToView:(UIView *)view;
-(void)scrollElement:(UIView *)view toPoint:(float)y;

@end

UIView+FormScroll.m:

UIView+FormScroll.m:

#import "UIView+FormScroll.h"


@implementation UIView (FormScroll)


-(void)scrollToY:(float)y
{

    [UIView beginAnimations:@"registerScroll" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];
    self.transform = CGAffineTransformMakeTranslation(0, y);
    [UIView commitAnimations];

}

-(void)scrollToView:(UIView *)view
{
    CGRect theFrame = view.frame;
    float y = theFrame.origin.y - 15;
    y -= (y/1.7);
    [self scrollToY:-y];
}


-(void)scrollElement:(UIView *)view toPoint:(float)y
{
    CGRect theFrame = view.frame;
    float orig_y = theFrame.origin.y;
    float diff = y - orig_y;
    if (diff < 0) {
        [self scrollToY:diff];
    }
    else {
        [self scrollToY:0];
    }

}

@end

将它导入到你的 UIViewController 中,然后你就可以了

Import that into your UIViewController, and then you can do

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.view scrollToView:textField];
}

-(void) textFieldDidEndEditing:(UITextField *)textField
{
    [self.view scrollToY:0];
    [textField resignFirstResponder];
}

...或者其他什么.该类别为您提供了三种非常好的调整视图位置的方法.

...or whatever. That category gives you three pretty good ways to adjust the position of a view.

这篇关于显示键盘时向上移动 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆