此问题与ios8的新自定义键盘无关,而与我为ios7应用程序创建的现有键盘有关。
从选项列表中选择用户时,可以使用UIPickerView
键盘。但是在某些情况下我只有两个选择,在这种情况下我不喜欢选择器视图。所以我创建了自己的UITableView
键盘。此键盘的大小取决于tableview中的行数(具有最大高度)。我在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;
答案 0 :(得分:1)
我已经让它再次运作,虽然我不明白旧的解决方案不起作用。所以,如果有人知道为什么......我仍然有兴趣了解更多。
解决方案:
- (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:
中viewWillAppear:
的来电。现在键盘按预期显示。
与ios7相比,我不得不改变另外一件事。我必须以编程方式设置表的行高。在xib文件中,行高已经被定义为44,但是当我用NSLog打印行高时,它给出的值为-1 ??