2

我正在使用以下方法为我的底部工具栏创建一些自定义 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,那么按钮会再次出现。

4

2 回答 2

0

使用setToolbarItemsUIViewController 中的方法设置工具栏按钮,这应该可以解决它。

于 2013-04-16T04:55:42.477 回答
0

我最终通过采用不同的方法解决了这个问题。我使用了标准的 UIToolbar,而不是导航控制器工具栏。使用 Interface Builder,我能够将一个普通的 UIView 添加到工具栏,然后在 UIView 中添加 2 个 UITextField。这样我就不必担心将文本字段添加到栏按钮项目内部的工具栏的所有代码 - 或者调用代码以在方向更改、隐藏和显示工具栏等时恢复文本字段。它结束了是一种更简单、更强大的方法。

于 2012-05-23T11:44:55.487 回答