Mad*_*han 5 objective-c back-button uibarbuttonitem
我想禁用导航控制器的默认后退按钮
self.navigationItem.rightBarButtonItem.enabled = NO;
// Below code does not work since leftBarButtonItem is always nil.
self.navigationItem.leftBarButtonItem.enabled = NO;
Run Code Online (Sandbox Code Playgroud)
我已经手动显示如下,但有没有任何属性禁用默认后退按钮只有一行?
backButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
[backButton setBackgroundImage:[UIImage imageNamed:@"backbutton_100.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@" All Customers" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[buttonView addSubview:backButton];
UIBarButtonItem* leftButton = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
// Now it is working.
self.navigationItem.leftBarButtonItem.enabled = NO;
Run Code Online (Sandbox Code Playgroud)
小智 11
它很容易.....只是试试这个
self.navigationController.navigationBar.userInteractionEnabled = NO; //for disabling
self.navigationController.navigationBar.userInteractionEnabled = YES; //for again enabling
Run Code Online (Sandbox Code Playgroud)
使用"hidesBackButton = YES"真的不是一个优雅的解决方案,因为它隐藏了不是我们想要的按钮.一个可接受的解决方法是将UILabel添加到后窗按钮上方的窗口,至少禁用按钮上的触摸.
将此方法添加到AppDelegate类:
- (void) disableLeftBarButtonItemOnNavbar:(BOOL)disable
{
static UILabel *l = nil;
if (disable) {
if (l != nil)
return;
l = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 160, 44)];
l.backgroundColor = [UIColor clearColor];
l.userInteractionEnabled = YES;
[self.window addSubview:l];
}
else {
if (l == nil)
return;
[l removeFromSuperview];
[l release];
l = nil;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以从任何视图控制器中调用它来禁用:
MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:YES];
Run Code Online (Sandbox Code Playgroud)
启用:
MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:NO];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7994 次 |
| 最近记录: |