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

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

问题描述

我知道这是一个经常出现的问题,但我没有从我找到的例子中实现这一点。我可能会做一些简单的错误..(希望如此)。我试图将UIview移动到隐藏在底部的文本字段。



关于我的应用程序,它有一个标签栏控制器并实现标准UIView 。当我添加代码移动显示时没有任何反应。我需要有一个滚动视图才能使用它,它是否与制表符控制器发生冲突或者我在做其他错误的操作?谢谢



Dan

解决方案

我在UIView上写了一个小类别管理暂时滚动的东西,而不需要将整个事物包装到UIScrollView中。我在这里使用动词scroll可能并不理想,因为它可能会让你认为有一个滚动视图,而且没有 - 我们只是动画UIView(或UIView子类)的位置。



这里嵌入了许多魔法数字,这些数字适合我的表格和布局,可能不适合你的,所以我鼓励调整它以满足你的特定需求。 / p>

UIView + FormScroll.h:

  #import< Foundation / Foundation.h> 

@interface UIView(FormScroll)

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

@end

UIView + FormScroll.m:

  #importUIView + FormScroll.h


@implementation UIView(FormScroll)


- (void)scrollToY:(float)y
{

[UIView beginAnimations:@registerScrollcontext: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,然后就可以了

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

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

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


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.

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

Dan

解决方案

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:

#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:

#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

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天全站免登陆