iOS7 initWithCustomView- leftBarButtonItem / rightBarButtonItem 偏移问题
2016-03-15 21:30
477 查看
-(void)initNavigationBar{
self.navigationItem.title = @"Create Message";
//leftBarButtonItem
UIButton * cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.0f, 44.0f)];
[cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
cancelBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.];
[cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:cancelBtn];
//verify whether system version >= 7 or not. for >= 7 , should cut negativeSpacer.width = -10;
if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7) {
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftBarBtn];
}else{
self.navigationItem.leftBarButtonItem = leftBarBtn;
}
//rightBarButtonItem
UIButton * rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.0f, 44.0f)];
[rightBtn setTitle:@"Publish" forState:UIControlStateNormal];
rightBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.];
[rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(rightBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
rightBarBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
//verify whether system version >= 7 or not. for >= 7 , should cut negativeSpacer.width = -10;
if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7) {
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.rightBarButtonItems = @[negativeSpacer, rightBarBtn];
}else{
self.navigationItem.rightBarButtonItem = rightBarBtn;
}
}
self.navigationItem.title = @"Create Message";
//leftBarButtonItem
UIButton * cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.0f, 44.0f)];
[cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
cancelBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.];
[cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:cancelBtn];
//verify whether system version >= 7 or not. for >= 7 , should cut negativeSpacer.width = -10;
if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7) {
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftBarBtn];
}else{
self.navigationItem.leftBarButtonItem = leftBarBtn;
}
//rightBarButtonItem
UIButton * rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.0f, 44.0f)];
[rightBtn setTitle:@"Publish" forState:UIControlStateNormal];
rightBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.];
[rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(rightBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
rightBarBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
//verify whether system version >= 7 or not. for >= 7 , should cut negativeSpacer.width = -10;
if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7) {
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.rightBarButtonItems = @[negativeSpacer, rightBarBtn];
}else{
self.navigationItem.rightBarButtonItem = rightBarBtn;
}
}
相关文章推荐
- iOS 数据存储--NSUserDefault
- ios基础(二)
- iOS 自定义对象转NSDictionary
- iOS中CocoaPods的安装和使用详解
- iOS多视图传值方式之通知传值(NSNotification;NSNotificationCenter)
- iOS开发地图定位及地理编码
- IOS逆向【5】GDB调试helloworld
- iOS下按钮同时实现圆角与阴影效果
- ios基础(一)
- iOS---Touch ID于密码的简易开发教程
- [iOS] 在循环中使用setNeedsDisplay来调用drawRect的方法
- ScrollView的contentSize属性 与 AutoLayout(Masonry布局)
- iOS中的事件传递和响应者链条
- iOS开发65-ASSERT(0)断言在iOS开发中的使用
- iOS开发之libcurl库的使用
- iOS 设计模式 --责任链模式
- iOS小技巧18-如何打包Ad Hoc ipa文件
- iOS 多视图—视图切换之代码块传参切换
- iOS 多视图—视图切换之代理Delegate传参切换
- iOS小技巧17-iPhone如何安装ipa包