gpt4 book ai didi

iphone - 返回选项卡后自定义导航栏无法正确显示

转载 作者:行者123 更新时间:2023-12-03 19:23:19 25 4
gpt4 key购买 nike

我在我正在创建的应用程序中的导航栏中添加了一些组件。当您单击 URL 字段时,栏的高度会动态变化,就像在 iPhone 的 Safari 中一样。除了当我移动到另一个选项卡并返回时,我的所有交互和调整大小都工作得很好。

如果我启动应用程序,请单击导航栏正确显示的第二个选项卡。

navbar OK

如果我立即单击第一个选项卡,然后再次返回第二个选项卡,导航栏将被剪切。

navbar clipped

我正在 viewWillAppear 方法中自定义导航栏

- (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];

if (navBar == nil) {

CGFloat width = self.view.frame.size.width;


navBar = [self.navigationController.navigationBar autorelease];
navBar.frame = CGRectMake(0,20,width,52);
//This doesn't appear to have any effect- was set to
//UIViewAutoresizingFlexibleWidth and worked but with with the same issue.
navBar.autoresizingMask = UIViewAutoresizingFlexibleHeight;

self.navigationItem.title = nil;


label = [[UILabel alloc] initWithFrame:
CGRectMake(10,2,width-20,14)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.text = @"My Custom NavBar";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:12];
label.textAlignment = UITextAlignmentCenter;
[navBar addSubview:label];

urlTextField = [[UITextField alloc] initWithFrame:
CGRectMake(10,19,width-75,26)];
urlTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
[urlTextField addTarget:(id)self action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];


bookmarkButton = [UIButton buttonWithType:UIButtonTypeCustom];
bookmarkButton.frame = CGRectMake(0,0,21,21);
[bookmarkButton setImage:[UIImage imageNamed:@"bookmark.png"] forState:UIControlStateNormal];
[bookmarkButton addTarget:self action:@selector(bookmarkPressed:) forControlEvents:UIControlEventTouchUpInside];
urlTextField.leftView = bookmarkButton;
urlTextField.leftViewMode = UITextFieldViewModeAlways;


actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
actionButton.frame = CGRectMake(0,0,21,21);
[actionButton setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
[actionButton addTarget:self action:@selector(actionPressed:) forControlEvents:UIControlEventTouchUpInside];
urlTextField.rightView = actionButton;
urlTextField.rightViewMode = UITextFieldViewModeAlways;


urlTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
urlTextField.borderStyle = UITextBorderStyleRoundedRect;
urlTextField.font = [UIFont systemFontOfSize:17];
urlTextField.delegate = self;
[navBar addSubview:urlTextField];


} else {

//XXX This does make the navBar display correctly
//but I don't really want the keyboard visible when returning from another tab
//[urlTextField becomeFirstResponder];

}

}

请注意上面代码中的最后注释 - 如果我强制显示键盘,或者如果用户在 UITextField 中单击,则导航栏将从其中正确显示。

我尝试了很多方法 - 检查 View (图像中的 View 2)、导航栏的帧大小,但没有成功。有谁知道问题可能是什么?我很乐意提供更多详细信息。

谢谢。

最佳答案

基本上你的处理方式都是错误的。您永远不应该访问导航栏的框架(更不用说更改它),并且您当然不应该直接向其添加 subview 。来自文档:

Unlike other types of views, you do not add subviews to a navigation bar directly. Instead, you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed.

因此,在您想要的 View Controller 的 -(id)initWithNibName:bundle: 方法中:

// Setting this will automatically grow the height of the navigation bar
// to the appropriate height
self.navigationItem.prompt = @"My Custom NavBar";

// Construct urlTextField here ...
self.navigationItem.titleView = urlTextField;

// This is the button with the gears icon
UIBarButtomItem *rightButtonItem = [[UIBarButtomItem alloc] initWithCustomView:view];
self.navigationItem.rightBarButtonItem = rightButtomItem;
[rightButtomItem release];

关于iphone - 返回选项卡后自定义导航栏无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2349113/

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