http://blog.csdn.net/jeffasd/article/details/50297363
// 设置左边按钮文字和点击事件
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"取消"
style:UIBarButtonItemStyleDone
target:self
action:@selector(cancel)];
// 设置右边按钮文字和点击事件
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"发表"
style:UIBarButtonItemStyleDone
target:self
action:@selector(post)];
// 设置title字体大少
NSMutableDictionary *items=[NSMutableDictionary dictionary];
items[NSForegroundColorAttributeName]=[UIColor whiteColor];//字体颜色
items[NSFontAttributeName]=[UIFont systemFontOfSize:17];//字体大小
[self.navigationController.navigationBar setTitleTextAttributes:items];
设置右边BarButtonItem的属性
// 设置item
// UIControlStateNormal正常情况下
NSMutableDictionary *itemAttrs = [NSMutableDictionary dictionary];
itemAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];//颜色,字体
itemAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:17];
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
// UIControlStateDisabled不能点击的情况下
NSMutableDictionary *itemDisabledAttrs = [NSMutableDictionary dictionary];//颜色,字体
itemDisabledAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:itemDisabledAttrs forState:UIControlStateDisabled];
当设置UIBarButtonItem的不同状态下的字体颜色或其他属性时,其按钮并不能入愿显示,这是因为navigationController刷新页面的比较慢,所以就不能显示。这时候需要把状态设置的代码都放到-viewDidAppear中
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationItem.rightBarButtonItem.enabled = NO; // 默认不能点击
// 强制刷新
[self.navigationController.navigationBar layoutIfNeeded];
}
设置导航栏的背景颜色:self.navigationController.navigationBar.barTintColor=[UIColor redColor];
//取消导航条对视图的影响:self.edgesForExtendedLayout = UIRectEdgeNone;
默认使用导航控制器包裹的控制器上第一个(从里到外,从上到下的添加顺序)UIScrollView或其子类会向下偏移64个点,也就是说它的bounds.origin.y = -64.
如果你不想让它偏移64,请在viewDidLoad设置
self.automaticallyAdjustsScrollViewInsets =NO;//默认是YES。
或者:因为系统会默认把View的contenInset的x值加上64,所以我们可以把View的contenInset设置成UIEdgeInsetsMake(-64, 0, 0, 0);