如果键盘阻塞文本视图,则移动 UIView



你好,我有一个UIView,它显示为模态。该视图包含一些 UITextView 字段。问题是当我编辑一些较低的视图时,键盘会在字段上弹出,隐藏它。

我需要一种方法来使视图内容可滚动,或者在键盘阻止视图内容时向上移动视图内容。视图以编程方式创建。我尝试移动视图,但我无法获取活动的文本视图(从不调用文本视图DidBeginEdit方法(。这是我的代码。

编辑控制器.m

- (void)editNavPressedEdit:(MapElement *)mapElement {
    [self dismissViewControllerAnimated:YES completion:^{
        EditAnnotationController *annotationController = [[EditAnnotationController alloc] init];
        annotationController.mapElement = mapElement;
        annotationController.delegate = self;
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:annotationController];
        navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
        navigationController.preferredContentSize = CGSizeMake(500, 500);
        navigationController.navigationBar.translucent = NO;
        navigationController.navigationBar.barTintColor = [UIColor blackColor];
        navigationController.view.backgroundColor = [UIColor blackColor];
        [self presentViewController:navigationController animated:YES completion:nil];
    }];
}

EditDetailController.h

@class MapElement;
@protocol EditAnnotationDelegate
- (void)editAnnotationSaved:(MapElement *)element;
- (void)editAnnotationCancelled;
@end
@interface EditAnnotationController : UIViewController <NavigationPickerDelegate, EditOverlayDelegate, UITextViewDelegate>
@property(nonatomic, strong) MapElement *mapElement;
@property(nonatomic, weak) id <EditAnnotationDelegate> delegate;
@end

EditDetailController.m

@implementation EditAnnotationController {
    UIView *_detailView;
    NSLayoutConstraint *_detailWidth;
    NSArray *_detailHoriz;
    NSMutableDictionary *_bindings;
    NSMutableArray *_detailObjects;
    NSDateFormatter *_dateFormatter;
    UIButton *_dateButton;
    UISegmentedControl *_segmentedControl;
    NSArray *_segments;
    NSString *_segmentsBinding;
    UITextField *_radiusField;
    UITextView *_activeField;
}
@synthesize mapElement = _mapElement;
@synthesize delegate = _delegate;
- (void)viewDidLoad {
    [super viewDidLoad];
    _detailObjects = [NSMutableArray array];
    _bindings = [NSMutableDictionary dictionary];
    _dateFormatter = [NSDateFormatter new];
    _dateFormatter.dateFormat = @"MM/dd/yyyy hh:mm:ss a";
    [self setUpDetailView];
    if (_mapElement) {
        [self setUpMapElement];
    }
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = saveButton;
    self.navigationItem.leftBarButtonItem = cancelButton;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)setUpDetailView {
    _detailView = [UIView new];
    _detailView.backgroundColor = [UIColor blackColor];
    _detailView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_detailView];
    NSArray *vertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:@{@"view": _detailView}];
    _detailHoriz = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:@{@"view": _detailView}];
    [self.view addConstraints:vertical];
    [self.view addConstraints:_detailHoriz];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.3 animations:^{
        CGRect f = self.view.frame;
        f.origin.y = -keyboardSize.height + 250;
        self.view.frame = f;
    }];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
    _activeField = textView;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    _activeField = nil;
}
- (void)keyboardWillHide:(NSNotification *)notification {
    [UIView animateWithDuration:0.3 animations:^{
        CGRect f = self.view.frame;
        f.origin.y = 44;
        self.view.frame = f;
    }];
}

- (void)setUpMapElement {
    [self setViewWidth:320];
    [self addImage:_mapElement.iconImage size:CGSizeMake(30, 30)];
    switch ([_mapElement.layerID intValue]) {
        case 1:
            [self configureHydrant];
            break;
    }
    [self addConstraints];
}

- (void)configureHydrant {
    [self addTitleLabel:@"Hydrant"];
    [self addHeaderLabel:@"Name:"];
    _bindings[propertyKeyPathLastComponent(_mapElement.subType)] = [self addTextField:_mapElement.subType];
    [self addHeaderLabel:@"Cap Color:"];
    _segments = @[@"Red", @"Blue", @"Green", @"Orange"];
    _segmentsBinding = propertyKeyPathLastComponent(_mapElement.steamerCapColor);
    [self addSegments:_segments selected:_mapElement.steamerCapColor];
    [self addHeaderLabel:@"Water line size:"];
    _bindings[propertyKeyPathLastComponent(_mapElement.waterLineSize)] = [self addTextField:_mapElement.waterLineSize.stringValue];
    [self addHeaderLabel:@"Flow rate:"];
    _bindings[propertyKeyPathLastComponent(_mapElement.flowRate)] = [self addTextField:_mapElement.flowRate.stringValue];
    [self addHeaderLabel:@"PSI:"];
    _bindings[propertyKeyPathLastComponent(_mapElement.psi)] = [self addTextField:_mapElement.psi.stringValue];
    [self addHeaderLabel:@"Notes:"];
    _bindings[propertyKeyPathLastComponent(_mapElement.description)] = [self addTextView:_mapElement.description];
}

每当您创建相关的文本视图时,请将您的视图控制器设置为它们的委托:

UITextField *myTextField = [UITextField alloc] init];
myTextField.delegate = self;

在此文本之后,应按预期调用视图DidBeginEdit。

首先注册"UIKeyboardWillShowNotification"和"UIKeyboardWillHideNotification"通知,如下所示,

[[NSNotificationCenter defaultCenter] addObserver:self                                           selector:@selector(keyboardWillShown:)
                                             name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil]

然后实现方法,'keyboardWillShow:"和'keyboardWillBeHidden:',如下所示,

- (void)keyboardWillShown:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
 if(selectedTextField.origin.y > self.view.size - kbSize.height){
     //shift view upwards
   }
 }
- (void)keyboardWillBeHidden:(NSNotification*)aNotification{
       //set origin of view to (0,0)
 }

最新更新