This question does not relate to the new custom keyboards for ios8, but to the existing keyboard I had created for my ios7 app.
When giving a choice to the user from a list of options, you can use the UIPickerView
for a keyboard. However in some cases I have only two choices and I don't like the picker view in that case. So I created my own UITableView
keyboard. The size of this keyboard depends on the number of rows in the tableview (with a maximum height). I set the height of the keyboard in ViewWilAppear
. This is working very well in ios7, but in ios8 the new height is not taken into account. I don't understand what is wrong. Can anyone help me to get this working again in ios8.
The keyboard in ios7 (sorry for the big picture, I don't know how to scale it):
The keyboard in ios8:
The code for my keyboard:
@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
EDIT as requested:
I create the keyboard as follows:
- (LBTableKeyBoardVC *)genderKeyBoard {
if (!_genderKeyBoard) {
_genderKeyBoard = [self tableKeyBoardWithData:@[NSLocalizedString(@"Male", "Gender keyboard option for male"), NSLocalizedString(@"Female", "Gender keyboard option for female")]];
}
return _genderKeyBoard;
}
I use the keyboard for a UITextField
in a custom UITableViewCell
:
textFieldCell.textField.inputView = self.genderKeyBoard.view;
I've got it working again, although I don't understand while the old solution is not working. So if someone knows why... I'm still interested to learn more.
Solution:
- (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;
}
I set the new height in setup
. Here I already know the data of the table and I can calculate the height for the view of the keyboard. I removed the call to setNewHeight:
in viewWillAppear:
. Now the keyboard appears as expected.
I had to change one other thing in comparison with ios7. I had to set the row height of the table programmatically. In the xib-file the row height was already defined as 44, but when I printed the row height with NSLog, it gave a value of -1??
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.