我正在使用以下方法为我的底部工具栏创建一些自定义 UIBarButtonItems:
- (void)initialisePageNoProperties
{
pageNoTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
[pageNoTextField setDelegate:self];
pageNoTextField.text = @"0";
pageNoTextField.textColor = [UIColor blackColor];
pageNoTextField.backgroundColor = [UIColor whiteColor];
pageNoTextField.textAlignment = UITextAlignmentRight;
pageNoTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
pageNoTextField.borderStyle = UITextBorderStyleRoundedRect;
pageNoTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
pageNoTextField.keyboardType = UIKeyboardTypeNumberPad;
pageNoTextField.returnKeyType = UIReturnKeyGo;
[pageNoTextField setClearsOnBeginEditing:YES];
pageNoBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:pageNoTextField];
pageNoBarButtonItem.style = UIBarButtonItemStyleBordered;
noOfPagesTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
noOfPagesTextField.text = @"0";
noOfPagesTextField.textColor = [UIColor blackColor];
noOfPagesTextField.backgroundColor = [UIColor clearColor];
noOfPagesTextField.textAlignment = UITextAlignmentLeft;
noOfPagesTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
noOfPagesTextField.enabled = NO;
noOfPagesBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:noOfPagesTextField];
}
然后使用以下方法将这些按钮添加到底部工具栏:
- (void)configurePageNoDisplay
{
if(![self.navigationController isToolbarHidden])
{
NSMutableArray *items = [[self.navigationController.toolbar items] mutableCopy];
bool insertIntoArray = ([items count] == 10); // without the page number display the items array will contain 10 items
if (insertIntoArray)
{
[items insertObject:pageNoBarButtonItem atIndex:3];
}
else
{
[items replaceObjectAtIndex:3 withObject:pageNoBarButtonItem];
}
if (insertIntoArray)
{
[items insertObject:noOfPagesBarButtonItem atIndex:4];
}
else
{
[items replaceObjectAtIndex:4 withObject:noOfPagesBarButtonItem];
}
[self.navigationController.toolbar setItems:items];
[self SetPageNoDisplay:[pdfViewCtrl GetCurrentPage]];
}
}
这些按钮的值设置如下:
- (void)SetPageNoDisplay:(NSInteger) pageNumber
{
pageNoTextField.text = [NSString stringWithFormat:@"%d", pageNumber];
noOfPagesTextField.text = [NSString stringWithFormat:@"of %d", [[pdfViewCtrl GetDoc] GetPageCount]];
}
按钮及其包含的字段声明如下:
@property (strong, nonatomic) IBOutlet UIBarButtonItem *pageNoBarButtonItem;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *noOfPagesBarButtonItem;
@property (strong, nonatomic) IBOutlet UITextField *pageNoTextField;
@property (strong, nonatomic) IBOutlet UITextField *noOfPagesTextField;
最初它们没有被声明为 IBOutlets - 但我读到了一个建议,这样做会帮助我解决问题 - 不幸的是它没有帮助。
我隐藏/显示底部工具栏以响应用户的双击:
- (void)handleDoubleTap:(UITapGestureRecognizer *)gestureRecognizer
{
[self.navigationController setNavigationBarHidden:![self.navigationController isNavigationBarHidden] animated:true];
[self.navigationController setToolbarHidden:![self.navigationController isToolbarHidden] animated:true];
[self configurePageNoDisplay];
//[sideBarTab setHidden:![sideBarTab isHidden]];
}
我遇到的问题是,一旦工具栏被隐藏 - 重新显示时按钮不会再次出现。如果我在重新显示工具栏后旋转 iPad,那么按钮会再次出现。