滚动隐藏在键盘上的内容. xcode 4.3.2 [英] Scrolling content hidden by keyboard. xcode 4.3.2

查看:87
本文介绍了滚动隐藏在键盘上的内容. xcode 4.3.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用xcode 4.3.2.我希望这是我最后一次要寻求帮助.

Using xcode 4.3.2. I hope this is the last time I have to beg for help.

我有一个包含4个文本字段的屏幕.编辑时,底部2被键盘隐藏.我希望能够向下滚动,以便可以编辑这两个隐藏字段.我已经粘贴了.m和.h文件.不太正确.

I have a screen with 4 text fields. The bottom 2 are hidden by the keyboard when editing. I want to be able to scroll down so these 2 hidden fields can be edited. I have pasted my .m and .h files. something is not quite right.

Viewcontroller.h

Viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollview;
@property (strong, nonatomic) IBOutlet UITextField *TextField1;
@property (strong, nonatomic) IBOutlet UITextField *TextField2;
@property (strong, nonatomic) IBOutlet UITextField *TextField3;
@property (strong, nonatomic) IBOutlet UITextField *TextField4;
@property (strong, nonatomic) IBOutlet UITextField *activefield;

- (IBAction)HideKeyBoard:(id)sender;

viewcontroler.m

viewcontroler.m

@interface ViewController ()
@end
@implementation ViewController

@synthesize scrollview;
@synthesize TextField1;
@synthesize TextField2;
@synthesize TextField3;
@synthesize TextField4;
@synthesize activefield;


 - (void)viewDidLoad

{   

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}



- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activefield = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activefield = nil;
}
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]
                     CGRectValue].size;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollview.contentInset = contentInsets;
    scrollview.scrollIndicatorInsets = contentInsets;
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activefield.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0,
                                          activefield.frame.origin.y-kbSize.height);
        [scrollview setContentOffset:scrollPoint animated:YES];

    }     

}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
  {
   UIEdgeInsets contentInsets = UIEdgeInsetsZero;
 scrollview.contentInset = contentInsets;
 scrollview.scrollIndicatorInsets = contentInsets;
 }


 - (void)viewDidUnload
 {

[self setScrollview:nil];
[self setActivefield:nil];
[self setTextField1:nil];
[self setTextField2:nil];
[self setTextField3:nil];
[self setTextField4:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
  return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)HideKeyBoard:(id)sender {

}


@end

推荐答案

我也遇到了这个问题.上面的链接确实帮助了我.希望对您有帮助

I have faced this problem too. The link above really helped me. I hope that it will help you too

在.h文件中,您必须创建一个像这样的属性

In the .h file you have to create a property like this

@property CGFloat animatedDistance;

然后在.m文件中,您需要添加以下内容:

then in the .m file you have to add this:

@synthesize animatedDistance;

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 = 162; 

然后您必须添加此方法:

then you have to add this method:

- (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);
}

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



- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

就是这样.祝你好运:)

That's it. Good luck : )

这篇关于滚动隐藏在键盘上的内容. xcode 4.3.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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