当键盘在文本字段上时,ios移动scrollview

前端之家收集整理的这篇文章主要介绍了当键盘在文本字段上时,ios移动scrollview前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
拜托,你能纠正我的代码,所以我有一个简单的应用程序示例,文本字段在键盘结束时向上移动吗?

我尝试使用来自ios开发人员库https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html代码实现它

但我不知道他们的意思是“活动字段存储在自定义变量中(在本例中称为activeField)”并且我可能做了其他错误.在viewWillAppear中使用registerForKeyboardNotifications是好的吗?

我知道有一些关于这个问题的线索,但我是新手,我很难理解它们.而且我不想只是学习如何,但为什么,这就是为什么我不想使用github其他建议等的现成的解决方案.

我的代码atm:

.H:

#import <UIKit/UIKit.h>

@interface VNViewController : UIViewController<UIScrollViewDelegate,UITextFieldDelegate>

@property (weak,nonatomic) IBOutlet UITextField *texticek;
@property (strong,nonatomic) IBOutlet UIScrollView *scrollView;

@end

.M:

#import "VNViewController.h"

@interface VNViewController ()

@end


@implementation VNViewController

@synthesize scrollView;
@synthesize texticek;

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self registerForKeyboardNotifications];
}

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// Call this method somewhere in your view controller setup code.
- (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 app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect,activeField.frame.origin) ) {
        [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
    }
}

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

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

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

@end

解决方法

单击时向上移动文本字段,请使用下面的代码.它只需要你的卷轴的插座
- (void)textFieldDidBeginEditing:(UITextField *)textField {

    self.scroll.contentOffset = CGPointMake(0,textField.frame.origin.y);
}

您可以通过减去y位置值来更改应显示的文本字段的位置(textfield.frame.origin.y – 某个值)

如果要为滚动设置动画,可以这样做:

CGPoint newOffset = CGPointMake(0,textField.frame.origin.y-40);
[self.scroll setContentOffset: newOffset animated: YES];
原文链接:https://www.f2er.com/iOS/333196.html

猜你在找的iOS相关文章