由于某种原因,导航栏中的右键距离右侧16px。我想把利润缩小。这样做的正确方法是什么?
self.btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *btnImgDone = [UIImage imageNamed:@"btn_small_default.png"];
self.btnDone.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15.0];
[self.btnDone setTitle:@"Done" forState:UIControlStateNormal];
[self.btnDone setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.btnDone setTitleShadowColor:[UIColor colorWithWhite:0 alpha:.2f] forState:UIControlStateNormal];
self.btnDone.titleLabel.shadowOffset = (CGSize){0,-1};
[self.btnDone setBackgroundImage:btnImgDone forState:UIControlStateNormal];
[self.btnDone setBackgroundImage:[UIImage imageNamed:@"btn_small_active.png"] forState:UIControlStateHighlighted];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.btnDone];
答案 0 :(得分:8)
我遇到了同样的问题,我发现修复它的唯一方法是使用setImageEdgeInsets:
。
如果要将按钮向右移动(例如:5分或10像素),请将以下行添加到按钮声明中:
UIEdgeInsets buttonEdges = UIEdgeInsetsMake(0, 5, 0, -5);
[self.btnDone setImageEdgeInsets:buttonEdges];
如果你想支持iOS6和iOS7,你可以这样做:
CGFloat xOffset;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
{
// for ios7
xOffset = 5.0f;
}
else
{
// ios6
xOffset = 2.0f;
}
UIEdgeInsets buttonEdges = UIEdgeInsetsMake(0, xOffset, 0, - xOffset);
[self.btnDone setImageEdgeInsets:buttonEdges];
希望它有所帮助!
答案 1 :(得分:6)
尝试在该按钮后放置一个固定的空格:
// Fixed space
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:20.0];
self.navigationItem.rightBarButtonItems = @[fixedSpace, yourButton];
我使用的整个代码是:
// Bar button
UIButton *loadButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 15, 34)];
VVdLoadImage *loadImage = [[VVdLoadImage alloc] initWithFrame:CGRectMake(0, 0, 15, 34)];
loadImage.backgroundColor = [UIColor clearColor];
loadImage.userInteractionEnabled = NO;
[loadButton addSubview:loadImage];
[loadButton addTarget:self action:@selector(loadCards) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *loadBarButton = [[UIBarButtonItem alloc] initWithCustomView:loadButton];
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:20.0];
self.navigationItem.rightBarButtonItems = @[fixedSpace, loadButton];
答案 2 :(得分:1)
见我的。
func addLogOutButtonToNavigationBar(triggerToMethodName: String)
{
let button: UIButton = UIButton()
button.setImage(UIImage(named: "logOff.png"), forState: .Normal)
button.frame = CGRectMake(20, 0, 30, 25)
button.contentEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: -10)
button .addTarget(self, action:Selector(triggerToMethodName), forControlEvents: UIControlEvents.TouchUpInside)
let rightItem:UIBarButtonItem = UIBarButtonItem()
rightItem.customView = button
self.navigationItem.rightBarButtonItem = rightItem
}
答案 3 :(得分:0)
如果你仍然无法让它工作,出于某种原因这对我有用。 someButton是我作为我的UIBarButtonItem的customView添加的UIButton
someButton.contentEdgeInsets = (UIEdgeInsets){.right=-20};