gpt4 book ai didi

ios - iPad - 隐藏 UIToolbar 后 UIBarButtonItem 消失

转载 作者:行者123 更新时间:2023-12-01 16:57:17 28 4
gpt4 key购买 nike

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

最佳答案

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

关于ios - iPad - 隐藏 UIToolbar 后 UIBarButtonItem 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10654629/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com