iOS 8更改键盘高度

伦蒂安

这个问题与ios8的新自定义键盘无关,但与我为ios7应用创建的现有键盘无关。

当从选项列表中为用户提供选择时,您可以将UIPickerView用作键盘。但是在某些情况下,我只有两种选择,在这种情况下,我不喜欢选择器视图。因此,我创建了自己的UITableView键盘。该键盘的大小取决于表格视图中的行数(最大高度)。我将键盘的高度设置为ViewWilAppear这在ios7中效果很好,但在ios8中,未考虑新高度。我不明白这是怎么回事。谁能帮助我在ios8中重新启用此功能。

ios7中的键盘(对不起,总的来说,我不知道如何缩放它): 在此处输入图片说明

ios8中的键盘: 在此处输入图片说明

我的键盘代码:

@interface LBTableKeyBoardVC () <UITableViewDelegate, UITableViewDataSource>

// List view in table form
@property (weak, nonatomic) IBOutlet UITableView *tableView;

// Last selected item in list
@property (strong, nonatomic) NSIndexPath *lastSelected;

// flag for using selecting mark when selecting a item
@property (nonatomic) BOOL selectionMark;

@end



@implementation LBTableKeyBoardVC

#define MAX_HEIGHT_KEYBOARD     245


#pragma mark - View initialization

- (id)initWithData:(NSArray *)data {
    // set data
    self.listData = data;

    // init view controller
    self = [self initWithNibName:@"LBTableKeyBoardVC" bundle:nil];

    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

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

    [self.tableView reloadData];

//    [self setNewHeight:[NSNumber numberWithInteger:(self.listData.count * self.tableView.rowHeight)]];
    [self performSelector:@selector(setNewHeight:) withObject:[NSNumber numberWithInteger:(self.listData.count * self.tableView.rowHeight)] afterDelay:0];
}

- (void)setup {
    // initialize table view
    self.tableView.delegate = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ListItem"];

    // resize keyboard if necessary
    // This is the code that will make sure the view does not get resized to the default keyboard frame size
    self.view.autoresizingMask = UIViewAutoresizingNone;
    self.tableView.rowHeight = 44;

    self.selectionMark = YES;
}

- (void)dealloc {
    self.tableView.delegate = nil;
    self.delegate = nil;
    self.listData = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)setNewHeight:(NSNumber *)height {
    NSInteger currentHeight = self.view.frame.size.height;
    NSInteger heightInt = [height integerValue];

    if (heightInt  != currentHeight) {
        if (heightInt > MAX_HEIGHT_KEYBOARD) {
            heightInt = MAX_HEIGHT_KEYBOARD;
        }

        self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, heightInt);
    }
}


#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"ListItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [self textForRowAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithHexString:@"C7CBD3"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectionMark) {
        // deselect old
        UITableViewCell *old = [self.tableView cellForRowAtIndexPath:self.lastSelected];
        old.accessoryType = UITableViewCellAccessoryNone;
        [old setSelected:NO animated:YES];

        // select new
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        [cell setSelected:YES animated:YES];

        // keep track of last selected
        self.lastSelected = indexPath;
    }

    [self.delegate keyboard:self.view selectedItem:[self textForRowAtIndexPath:indexPath]];
}


#pragma mark - List data

- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.listData objectAtIndex:indexPath.row];
}

@end

根据要求编辑:

我按如下方式创建键盘:

- (LBTableKeyBoardVC *)genderKeyBoard {
    if (!_genderKeyBoard) {
        _genderKeyBoard = [self tableKeyBoardWithData:@[NSLocalizedString(@"Male", "Gender keyboard option for male"), NSLocalizedString(@"Female", "Gender keyboard option for female")]];
    }

    return _genderKeyBoard;
}

UITextField在自定义中使用键盘UITableViewCell

textFieldCell.textField.inputView = self.genderKeyBoard.view;
伦蒂安

尽管旧的解决方案无法正常运作,但我还是不明白,尽管它仍然可以正常工作。所以,如果有人知道为什么...我仍然有兴趣了解更多信息。

解决方案:

- (void)setup {
    // initialize table view
    self.tableView.delegate = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ListItem"];

    // resize keyboard if necessary
    // This is the code that will make sure the view does not get resized to the default keyboard frame size
    self.view.autoresizingMask = UIViewAutoresizingNone;
    self.tableView.rowHeight = KEYBOARD_ROWHEIGHT;

    // set height of keyboard view according the number of rows in the table
    [self setNewHeight:[NSNumber numberWithInteger:(self.listData.count * self.tableView.rowHeight)]];

    self.selectionMark = YES;
}

我在中设置了新的高度setup在这里,我已经知道表格的数据,并且可以计算键盘视图的高度。我删除了对setNewHeight:in的呼叫viewWillAppear:现在,键盘按预期出现。

与ios7相比,我不得不改变另一件事。我必须以编程方式设置表格的行高。在xib文件中,行高已经定义为44,但是当我使用NSLog打印行高时,它的值为-1?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS 8更改键盘高度

来自分类Dev

iOS 8自定义键盘:更改高度

来自分类Dev

iOS 8获取键盘高度

来自分类Dev

如何使用Swift更改iOS 8自定义键盘高度?

来自分类Dev

iOS 8自定义键盘:更改高度而未警告“无法同时满足约束条件...”

来自分类Dev

iOS 8自定义键盘的高度从纵向更改为横向,然后再横向更改为纵向

来自分类Dev

Phonegap:键盘更改iOS 7中的窗口高度

来自分类Dev

在iOS 8中更改formSheet的高度

来自分类Dev

iOS键盘高度不同

来自分类Dev

iOS键盘高度不同

来自分类常见问题

在iOS8中无法获取正确的键盘高度值

来自分类Dev

iOS 8自定义键盘方向更改

来自分类Dev

iOS8-更改方向键盘后消失

来自分类Dev

iOS 8键盘扩展检测第一响应者更改

来自分类Dev

在iOS7 / 8中更改UITabBar的高度?

来自分类Dev

在iOS7 / 8中更改UITabBar的高度?

来自分类Dev

在iOS 8的自定义键盘中检测键盘类型的更改

来自分类Dev

iOS8-具有动态高度的键盘输入附件视图

来自分类Dev

iOS8 UIKeyboardWillShowNotification第三方键盘高度

来自分类Dev

在iOS中更改UISegmentedControl的高度

来自分类Dev

iOS 7:更改UIAlertView的高度

来自分类Dev

iOS8键盘方向

来自分类Dev

在不显示键盘的情况下获取iOS键盘的高度

来自分类Dev

在iOS 7下,如何使UITextView的屏幕高度减去键盘的高度?

来自分类Dev

如何在iOS 8的自定义键盘扩展中检测方向更改?

来自分类Dev

iOS 8自定义键盘在“设置”页面中更改语言

来自分类Dev

iOS 8方向更改:键盘框架无法正确显示

来自分类Dev

旋转时处理更改的iOS键盘布局

来自分类Dev

旋转时处理更改的iOS键盘布局