我正在尝试使用自定义控件实现 UINavigationBar。因此,我在 UINavigationBar 的左侧添加了 UIView,并尝试使用以下代码向该 UIView 添加控件:
UIView *view = navigationController.navigationItem.leftBarButtonItem.customView;
UIButton *button = [[UIButton alloc] initWithFrame:view.frame];
[button setTitle:@"TEST" forState:UIControlStateNormal];
[view addSubview:button];
[button sizeToFit];
[button release];
代码有效,但没有出现按钮。
任何帮助,将不胜感激。谢谢你。
UPD
好的,我尝试在下面编写代码并做了这样的事情:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 50)];
[button setTitle:@"XXX" forState:UIControlStateNormal];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
navigationController.visibleViewController.navigationItem.leftBarButtonItem = customItem;
[customItem release];
[button release];
“XXX”标题确实出现了,但它看起来像简单的标签,而不是按钮。有任何想法吗?