Objective-C:由UIViewController中出现的键盘隐藏的UIScroller和UItextfields [英] Objective-C:UIScroller and UItextfields hidden by keyboard appearing in UIViewController

查看:140
本文介绍了Objective-C:由UIViewController中出现的键盘隐藏的UIScroller和UItextfields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助尝试在UIViewController中配置键盘:(a)不要悬停在两个UITextfields上,因此滚动条应该正确定位; (b)当用户触摸背景时,键盘消失。

I need help trying to configure in a UIViewController, the keyboard : (a) not hovering over both UITextfields, so the scroller should be positioned correctly; and (b) When the user touches the background the keyboard disappears.

我还没试过(b)但是我正在尝试(a)和我从google搜索到的代码没有给我预期的效果。事实上,当我触摸其中一个时,它会使我的文本框消失。我确定我的activeField实现也是错误的。我从Apple Development的键盘部分得到了这个例子。

I havent tried (b) yet but im trying (a) and the code I got from googling around didn't give me the desired effects. In-fact, it makes my textboxes dissapear when I touch one of them. Im sure my implementation of activeField is also wrong. I got this example from Apple Development's Keyboard section.

任何帮助都将不胜感激。
谢谢

Any help would be appreciated. Thanks

@interface FirstViewController : UIViewController {
    IBOutlet UITextField *emailAddress;
    IBOutlet UITextField *password;
    IBOutlet UIButton *loginButton;
    IBOutlet UIScrollView *scroller;
    BOOL keyboardShown;
    UITextField *activeField;

    ASIHTTPRequest *requestRequiringAuthentication;
    ASINetworkQueue *networkQueue;
}

- (IBAction) LoginUser:(id)sender;

@property (nonatomic,retain) IBOutlet UITextField *emailAddress;
@property (nonatomic,retain) IBOutlet UITextField *password;
@property (nonatomic, retain) IBOutlet UIScrollView *scroller;
@property (nonatomic, retain) UITextField *activeField;
@property (retain) ASINetworkQueue *networkQueue;
@property (retain) ASIHTTPRequest *requestRequiringAuthentication;

@end


@implementation FirstViewController

@synthesize requestRequiringAuthentication;
@synthesize networkQueue;
@synthesize password;
@synthesize emailAddress;
@synthesize scroller;
@synthesize activeField;

#pragma mark -
#pragma mark LifeCycle

- (void)awakeFromNib
{
    NSLog(@"awaking from nib");
    [self setNetworkQueue:[[[ASINetworkQueue alloc] init] autorelease]];    
}    

- (void)viewDidLoad {
    NSLog(@"viewdidload");
    [super viewDidLoad];
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    keyboardShown = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
[super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super viewWillDisappear:animated];
}


// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    if (keyboardShown)
        return;

    NSDictionary* info = [aNotification userInfo];

    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    // Resize the scroll view (which is the root view of the window)
    CGRect viewFrame = [scroller frame];
    viewFrame.size.height -= keyboardSize.height;
    scroller.frame = viewFrame;

    // Scroll the active text field into view.
    CGRect textFieldRect = [activeField frame];
    [scroller scrollRectToVisible:textFieldRect animated:YES];

    keyboardShown = YES;
}

// Called when the UIKeyboardDidHideNotification is sent
- (void)keyboardWasHidden:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];

    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    // Reset the height of the scroll view to its original value
    CGRect viewFrame = [scroller frame];
    viewFrame.size.height += keyboardSize.height;
    scroller.frame = viewFrame;

    keyboardShown = NO;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}


推荐答案

我在下面找到了以下代码博客条目(我不知道哪一个,因为我认为我在转换到Google Reader同步之前已将其保存在NNW中)。它都存在于实现文件中,只需使您的类成为UITextFields的委托,并将其声明为标头中的UITextFieldDelegate协议。我直接从我的一个发货应用程序的代码中删除了它,它非常适合将文本字段移出键盘。我还没有设置你的(B),但我相信它只是另一个需要实现的UITextFieldDelegate方法。

I found the following code in a blog entry (I don't know which one because I think I had it saved in NNW before the switch to Google Reader syncing). It all lives in the implementation file, just make your class the delegate for your UITextFields, and declare it as following the UITextFieldDelegate protocol in your header. I pulled this straight out of the code for one of my shipping apps, and it works great for moving the text fields out of the way of the keyboard. I haven't setup your (B) yet, but I believe it's just another UITextFieldDelegate method that needs to be implemented.

为你的类设置以下常量:

Setup the following constants for your class:

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 140;

然后添加以下两种方法:

And then add these two methods:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
   CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator =
            midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator =
            (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;

   if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

   UIInterfaceOrientation orientation =
   [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }
    CGFloat heightFraction = numerator / denominator;

   if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

   UIInterfaceOrientation orientation =
   [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

   CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}

这篇关于Objective-C:由UIViewController中出现的键盘隐藏的UIScroller和UItextfields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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