UITableView/UICollectionView键盘问题

UITableView/UICollectionView键盘问题_第1张图片
CartKeyBoard.png

滚动视图防止遮挡以及退键盘

某控制器中含有tableView,其cell上放置textField(textView),点击后弹出键盘,并且自动滚动表格防止遮挡,完成要收键盘。

#define DDYSCREENW [UIScreen mainScreen].bounds.size.width
#define DDYSCREENH [UIScreen mainScreen].bounds.size.height

@interface NACartController ()
/** 展示表格 */
@property (nonatomic, strong) UITableView *tableView;
/** 用来回收键盘 */
@property (nonatomic, strong) UIView *keyboardView;
@end


@implementation NACartController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTabelView];
    [self setupKeyBoardBackView];
    [self addKeyboardNotifications];
}

#pragma mark 添加表格视图
- (void)setupTabelView
{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.tableFooterView = [UIView new];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = ORDER_BACK_COLOR;
    [self.view addSubview:_tableView];
}

#pragma mark - UITabelViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NAShoppingHeaderModel *headerModel = self.groupArray[section];
    NSArray *goods = headerModel.goodsInfo;
    return goods.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NAShoppingCell *cell = [NAShoppingCell cellWithTableView:tableView];
    cell.model = self.modelArray[indexPath.section][indexPath.row];
    cell.cellType = ShoppingCellTypeDefault;
    cell.delegate = self;
    __weak __typeof__ (self)weakSelf = self;
    cell.textFieldIndexPathBlock = ^() {
        [weakSelf performSelector:@selector(scrollToCell:) withObject:indexPath afterDelay:0.3f];
    };
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NAShoppingHeaderModel * headModel = self.groupArray[section];
    NAShoppingHeader * headView = [[NAShoppingHeader alloc] initWithSection:section HeadModel:headModel];
    headView.delegate = self;
    return headView;
}

#pragma mark - UITabelViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

#pragma mark - 键盘问题

#pragma mark 添加确定取消回收键盘视图
- (void)setupKeyBoardBackView {
    _keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, DDYSCREENH, DDYSCREENW, 28)];
    _keyboardView.backgroundColor = DDYColor(250, 250, 250, 1);
    _keyboardView.hidden = YES;
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    cancelBtn.contentMode = UIViewContentModeLeft;
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    cancelBtn.frame = CGRectMake(10, 0, 35, 28);
    [cancelBtn addTarget:self action:@selector(handleCancel:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    okBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    okBtn.contentMode = UIViewContentModeRight;
    okBtn.frame = CGRectMake(DDYSCREENW-45, 0, 35, 28);
    [okBtn setTitle:@"确定" forState:UIControlStateNormal];
    [okBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [okBtn addTarget:self action:@selector(handleOK:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_keyboardView];
    [_keyboardView addSubview:cancelBtn];
    [_keyboardView addSubview:okBtn];
    [self.view bringSubviewToFront:_keyboardView];
}

#pragma mark 键盘通知
-(void)addKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark 键盘弹出
- (void)keyboardWillShow:(NSNotification *)notification
{
    _keyboardView.hidden = NO;
    NSDictionary* info = [notification userInfo];
    CGFloat kbh = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    [UIView animateWithDuration:0.2 animations:^{
        _keyboardView.ddy_y = self.view.ddy_h-kbh-28;
        _tableView.ddy_h = self.view.ddy_h-kbh-28;
    }];
}

#pragma mark 键盘收回
- (void)keyboardWillHide:(NSNotification *)notification
{
    
    [UIView animateWithDuration:0.2 animations:^{
        _tableView.ddy_h = self.view.ddy_h-44;
        _keyboardView.ddy_y = DDYSCREENH;
    } completion:^(BOOL finished) {
        _keyboardView.hidden = YES;
    }];
}

#pragma mark 点击取消
- (void)handleCancel:(UIButton *)sender
{
    [self.view endEditing:YES];
}

#pragma mark 点击确定
- (void)handleOK:(UIButton *)sender
{
    [self.view endEditing:YES];
}
- (void)scrollToCell:(NSIndexPath*) path {
    [_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
}

#pragma mark 移除通知
- (void)dealloc
{
    _tableView.delegate = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

cell利用textField的代理

// .h
typedef void (^indexPathOfTextFieldBlock)(void);

@property (nonatomic, copy) indexPathOfTextFieldBlock textFieldIndexPathBlock;

// .m
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (self.textFieldIndexPathBlock) {
        self.textFieldIndexPathBlock();
    }
}

滚动到底部

- (void)scrollToBottom
{
    CGFloat yOffset = 0; //设置要滚动的位置 0最顶部 CGFLOAT_MAX最底部
    if (self.tableView.contentSize.height > self.tableView.bounds.size.height) {
        yOffset = self.tableView.contentSize.height - self.tableView.bounds.size.height;
    }
    [self.tableView setContentOffset:CGPointMake(0, yOffset) animated:NO];
}

滚动到fo'o'te'r

键盘类型

typedef NS_ENUM(NSInteger, UIKeyboardType) {
    UIKeyboardTypeDefault,                // Default type for the current input method.
    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.
    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
};
  • UIKeyboardTypeDefault,常用于文本输入
UITableView/UICollectionView键盘问题_第2张图片
UIKeyboardTypeDefault.png
  • UIKeyboardTypeASCIICapable,常用于密码输入
UITableView/UICollectionView键盘问题_第3张图片
UIKeyboardTypeASCIICapable.png
  • UIKeyboardTypeNumbersAndPunctuation,和上一个键盘互相切换
UITableView/UICollectionView键盘问题_第4张图片
UIKeyboardTypeNumbersAndPunctuation.png
  • UIKeyboardTypeURL,适用于网址输入
UITableView/UICollectionView键盘问题_第5张图片
UIKeyboardTypeURL.png
  • UIKeyboardTypeNumberPad ,只有数字的数字键盘
UITableView/UICollectionView键盘问题_第6张图片
UIKeyboardTypeNumberPad.png
  • UIKeyboardTypePhonePad,可用于拨号的数字键盘,带*#+
UITableView/UICollectionView键盘问题_第7张图片
UIKeyboardTypePhonePad.png
  • UIKeyboardTypeNamePhonePad,字母及数字键盘,主次键盘分别如下:
UITableView/UICollectionView键盘问题_第8张图片
UIKeyboardTypeNamePhonePad.png
UITableView/UICollectionView键盘问题_第9张图片
UIKeyboardTypeNamePhonePad2.png
  • UIKeyboardTypeEmailAddress,适用于邮件地址输入的键盘
UITableView/UICollectionView键盘问题_第10张图片
UIKeyboardTypeEmailAddress.png
  • UIKeyboardTypeDecimalPad,带“点”的数字键盘,可用于带有小数点的数字输入
UITableView/UICollectionView键盘问题_第11张图片
UIKeyboardTypeDecimalPad.png
  • UIKeyboardTypeTwitter
UITableView/UICollectionView键盘问题_第12张图片
UIKeyboardTypeTwitter.png
  • UIKeyboardTypeWebSearch,适用于网页搜索的键盘
UITableView/UICollectionView键盘问题_第13张图片
UIKeyboardTypeWebSearch.png

大写类型

typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {
    UITextAutocapitalizationTypeNone,  // 不大写
    UITextAutocapitalizationTypeWords, // 单词首字母
    UITextAutocapitalizationTypeSentences, // 句子首字母
    UITextAutocapitalizationTypeAllCharacters, // 全大写字母
};

键盘外观

typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
    UIKeyboardAppearanceDefault,          // Default apperance for the current input method.
    UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
    UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
    UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,  // Deprecated
};

拼写检查

typedef NS_ENUM(NSInteger, UITextSpellCheckingType) {
    UITextSpellCheckingTypeDefault,
    UITextSpellCheckingTypeNo,
    UITextSpellCheckingTypeYes,
} NS_ENUM_AVAILABLE_IOS(5_0);

return类型

typedef NS_ENUM(NSInteger, UIReturnKeyType) {
    UIReturnKeyDefault,
    UIReturnKeyGo,
    UIReturnKeyGoogle,
    UIReturnKeyJoin,
    UIReturnKeyNext,
    UIReturnKeyRoute,
    UIReturnKeySearch,
    UIReturnKeySend,
    UIReturnKeyYahoo,
    UIReturnKeyDone,
    UIReturnKeyEmergencyCall,
    UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
};

你可能感兴趣的:(UITableView/UICollectionView键盘问题)