2014-09-27 115 views
1

此問題與ios8的新自定義鍵盤無關,而與我爲ios7應用程序創建的現有鍵盤無關。IOS 8更改鍵盤高度

當從選項列表中給用戶一個選擇時,可以使用UIPickerView作爲鍵盤。但是在某些情況下,我只有兩種選擇,在這種情況下我不喜歡選擇器視圖。所以我創建了我自己的UITableView鍵盤。此鍵盤的大小取決於tableview中的行數(最大高度)。我在ViewWilAppear中設置鍵盤的高度。這在ios7中運行得非常好,但在ios8中並沒有考慮到新的高度。我不明白什麼是錯的。任何人都可以幫助我在ios8中再次使用它。

鍵盤在ios7(抱歉的大圖片,我不知道如何縮放): enter image description here

鍵盤在iOS8上: enter image description here

我的鍵盤代碼:

根據要求
@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

你能分享顯示鍵盤的代碼嗎? – Rikkles 2014-09-27 10:38:08

回答

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設置新的高度。在這裏我已經知道表格的數據,並且我可以計算鍵盤視圖的高度。我在viewWillAppear:中刪除了撥打setNewHeight:的電話。現在鍵盤按預期顯示。我不得不改變另一件事與ios7相比。我不得不以編程方式設置表格的行高度。在xib文件中,行高已經定義爲44,但是當我用NSLog打印行高時,它給出了-1?