出于某种原因,导航栏中的右键距离右侧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];
Run Code Online (Sandbox Code Playgroud)

我有同样的问题,我发现解决它的唯一方法是使用setImageEdgeInsets:.
如果要将按钮向右移动(例如:5 pts或10 px),请将以下行添加到按钮声明中:
UIEdgeInsets buttonEdges = UIEdgeInsetsMake(0, 5, 0, -5);
[self.btnDone setImageEdgeInsets:buttonEdges];
Run Code Online (Sandbox Code Playgroud)
如果你想支持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];
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你 !
尝试在该按钮后放置一个固定的空格:
// Fixed space
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:20.0];
self.navigationItem.rightBarButtonItems = @[fixedSpace, yourButton];
Run Code Online (Sandbox Code Playgroud)
我用过的整个代码是:
// 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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8669 次 |
| 最近记录: |