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

发布于 2024-08-23 14:08:01 字数 3438 浏览 4 评论 0原文

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

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

navbar OK

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

导航栏剪裁

我' m 在 vi​​ewWillAppear 方法中自定义导航栏

- (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 中单击,则导航栏将从其中正确显示。

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

谢谢。

I've added a few components to the NavigationBar in an app I'm creating. The height of the bar changes dynamically, like in Safari for the iPhone, when you click on the URL field. I have all of the interactions and resizings working perfectly, except when I move to another tab and return.

If I start the app, click on second tab the navbar displays correctly.

navbar OK

If I then immediately click on the first tab, and then back to the second tab again, the NavigationBar is clipped.

navbar clipped

I'm customizing the navbar in my viewWillAppear method

- (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];

    }

}

Note the final comment in the code above- If I force the keyboard to display, or if the user clicks in the UITextField, the NavBar is displayed correctly from therein.

I've tried a lot of things- checking the frames sizes of the View (View 2 in the images), the NavBar, but with no luck. Does any one have any idea what the problem might be? I'll be happy to give further detail.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-08-30 14:08:01

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

与其他类型的视图不同,您可以
不将子视图添加到导航栏
直接地。相反,您使用
导航项(一个实例
UINavigationItem 类)来指定
您想要什么按钮或自定义视图
显示。

因此,在视图控制器的 -(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];

Basically you're going about this all the wrong way. You should never have to access the frame of the navigation bar (let alone change it) and you should certainly never add subviews to it directly. From the docs:

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.

So in the -(id)initWithNibName:bundle: method of your view controller you want:

// 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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文