返回选项卡后自定义导航栏无法正确显示
我在我正在创建的应用程序中向导航栏添加了一些组件。当您单击 URL 字段时,栏的高度会动态变化,就像在 iPhone 的 Safari 中一样。我的所有交互和大小调整都完美运行,除非我移动到另一个选项卡并返回。
如果我启动应用程序,请单击导航栏正确显示的第二个选项卡。
如果我然后立即单击第一个选项卡,然后再次返回第二个选项卡,导航栏被剪切。
我' m 在 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 中单击,则导航栏将从其中正确显示。
我已经尝试了很多方法 - 检查视图(图像中的视图 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.
If I then immediately click on the first tab, and then back to the second tab again, the NavigationBar is 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上你的处理方式都是错误的。您永远不应该访问导航栏的框架(更不用说更改它),并且您当然不应该直接向其添加子视图。来自文档:
因此,在视图控制器的 -(id)initWithNibName:bundle: 方法中,您需要:
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:
So in the -(id)initWithNibName:bundle: method of your view controller you want: