我使用以下方法为底部工具栏创建一些自定义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,则按钮会再次出现。
答案 0 :(得分:0)
我最后通过采用不同的方法来解决这个问题。我使用标准的UIToolbar,而不是导航控制器工具栏。使用Interface Builder我能够在工具栏中添加一个普通的UIView,然后在UIView中添加2个UITextField。这样我就不必担心所有代码都将文本字段添加到栏按钮项内的工具栏中 - 或者调用代码来恢复方向更改,隐藏和显示工具栏等文本字段。结果是一种更简单,更强大的方法。
答案 1 :(得分:0)
使用UIViewController中的setToolbarItems
方法设置工具栏按钮,并解决它。