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

标签 iphone cocoa-touch uinavigationcontroller uitabbarcontroller

我在我正在创建的应用程序中的导航栏中添加了一些组件。当您单击 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/

相关文章:

iphone - (viewConroller.view removeFromSuperview) 线程 :1 EXC_BAD_ACCESS (Code=1, 地址 = 0x6000000008)

iphone - CGContextClip空路径错误(自定义UINavigationBar背景)

IOS - Swift - UINavigationController.hidesBarsOnSwipe 错误

iphone - 双击 UITabBarController 选项卡转到导航 Controller 的根目录

iphone - 如何从 UITableView 中删除部分 (iPhone/iPad)

iphone - viewDidLoad(), 加载 View ()

objective-c - 选项卡式 View Controller ,在另一个 View 中更改文本

cocoa-touch - Xcode 6 Cocoa Touch 框架项目找不到公共(public) header

ios - 在 UIToolbar 上创建左箭头按钮(如 UINavigationBar 的 "back"样式)

ios - 将数据从一个 View Controller 传递到另一个; iOS <=4 对比 iOS 5