我可以实现导航栏并添加左栏按钮,但是在左按钮有空间之前,请询问如何在ios 7中删除这个空间。
问问题
3071 次
3 回答
4
使用它会起作用我已经使用过它并且在iOS 7上也可以正常工作
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithCustomView:segmentView];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -6;// it was -6 in iOS 6 you can set this as per your preference
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,homeButton, nil] animated:NO];
于 2013-11-13T12:48:04.830 回答
0
我们可以添加自定义导航栏的左栏按钮,但在左按钮之前有空间。我解决了这个问题如下,
来电:
// MyViewController.m
- (void)viewDidLoad {
UIBarButtonItem *bbBack = [Utilities overrideBackBarButtonItemWithTarget:self action:@selector(btnBackTapped:)];
}
- (void)btnBackTapped:(id)sender {
//Custom navigation back bar button item
}
将此类方法添加到您的 Utilities 类或项目的任何公共类中,
执行:
// Utilities.h
@interface Utilities : NSObject
#pragma mark - back bar button item
+(UIBarButtonItem*)overrideBackBarButtonItemWithTarget:(nullable id)target action:(nullable SEL)action;
@end
// Utilities.m
@implementation Utilities
+(UIBarButtonItem*)overrideBackBarButtonItemWithTarget:(nullable id)target action:(nullable SEL)action {
// back button
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65 , 44)];
[btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
btn.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);//move image to the right
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbuttonBack = [[UIBarButtonItem alloc] initWithCustomView:btn];
//navigation spacer
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spacer.width = -15;// it was -6 in iOS 6 you can set this as per your preference
//set leftBarButtonItems
UIViewController *_self = (UIViewController*)target;
[_self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:spacer,barbuttonBack, nil] animated:NO];
return barbuttonBack;
}
@end
返回图片: back.png
于 2017-05-17T08:52:52.623 回答
0
你可以使用一个系统.fixedSpace
UIBarButtonItem
。
这是产生预期结果的快速版本(iOS 10+):
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -20 // working on iOS 10.
let barButton = UIBarButtonItem(title: "<# title #>", style: .plain, target: <# target #> , action: #selector(<# action #>))
navigationItem.leftBarButtonItems = [negativeSpacer, barButton]
另一个想法是直接添加UIButton
一个UINavigationController.view
。
于 2017-03-08T09:33:52.707 回答