UIButton *btn=[[UIButton alloc]init]; btn.frame = CGRectMake(0, 0, 40, 20); btn.backgroundColor = [UIColor redColor]; UIButton *btn2=[[UIButton alloc]init]; btn2.frame = CGRectMake(0, 0, 40, 20); btn2.backgroundColor = [UIColor blueColor]; 设置按钮的背景图片(默认/高亮) [btn setBackgroundImage:[UIImage imageWithName:ImageName] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageWithName:highImageName] forState:UIControlStateHighlighted]; 设置按钮的尺寸和图片一样大,使用了UIImage的分类 [btn addTarget:self action:@"close" forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *btnItem1 = [[UIBarButtonItem alloc]initWithCustomView:btn]; UIBarButtonItem *btnItem2 = [[UIBarButtonItem alloc]initWithCustomView:btn2]; //显示右边的按钮 self.navigationItem.leftBarButtonItem = btnItem1;//ok self.navigationItem.hidesBackButton = YES; //隐藏左边的返回按钮 self.navigationItem.leftBarButtonItem = nil; //仍然显示返回 self.navigationItem.rightBarButtonItem = btnItem2;//ok
如何在iOS 7中设置barTintColor实现类似网易和 Facebook 的 navigationBar 效果
隐藏leftbar
self.navigationItem.leftBarButtonItem.customView.hidden = YES; //隐藏leftbar
隐藏导航栏
self.navigationController.navigationBar.hidden = YES;
调整导航条上leftBarButtonItem和rightBarButtonItem与屏幕边界的间距
再说说iOS的状态栏(显示与隐藏)
iOS_隐藏顶部状态栏方式
iOS 6通过[UIApplication sharedApplication] 取得app的单例,然后调用setStatusBarHidden方法隐藏 Status Bar。
iOS 7
Info.plist
不但要在Info.plist中增加 Status bar is initially hidden一行,选择为 YES,
还需增加 View controller-based status bar appearance 一行,选择为 NO。