当我尝试以下代码时,条形按钮与边缘间距约为30px,
但我希望距离边缘只有2个像素。有没有办法呢?
我只想要条形按钮,而不是自定义UIButton。
UIImage* buttonImage = [UIImage imageNamedH568: @"publish_button.png"];
UIButton * clearButton = [UIButton buttonWithType: UIButtonTypeCustom];
clearButton.frame = CGRectMake(244.0f, 5.0f, 65.0f, 33.0f);
clearButton.titleLabel.font = [Utils helveticaBoldFontOfSize: 12.0f];
[clearButton setBackgroundImage: buttonImage forState: UIControlStateNormal];
[clearButton setTitle: NSLocalizedString(@"Clear", nil) forState: UIControlStateNormal];
[clearButton setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[clearButton addTarget: self action: @selector(clearNotificationsList:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem* clearItem = [[UIBarButtonItem alloc] initWithCustomView: clearButton];
self.navigationItem.rightBarButtonItem = clearItem;
答案 0 :(得分:3)
- (UIBarButtonItem *)spacer
{
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = -11;
return space;
}
- (BOOL)isIOS7
{
return ([[[UIDevice currentDevice] systemVersion] compare:@"7" options:NSNumericSearch] != NSOrderedAscending);
}
然后在loadView中使用以下内容 -
UIBarButtonItem* clearItem = [[UIBarButtonItem alloc] initWithCustomView: clearButton];
if ([self isIOS7]) {
self.navigationItem.rightBarButtonItems = @[[self spacer],clearItem];
}
else
{
self.navigationItem.rightBarButtonItem = clearItem;
}
完美无缺地工作
谢谢大家的帮助!
答案 1 :(得分:0)
设置框架不会起作用。
您必须对图像使用以下内容:
[clearButton setBackgroundImage:[[UIImage imageNamed:@"publish_button"] imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, - 10 : 0 , 0, 0)] forState:UIControlStateNormal];// -10 or something that suits you.
标题如下:
clearButton.titleEdgeInsets = UIEdgeInsetsMake(0, - 10, 0, 0);
编辑:
或者,您可以使用以下两者:
clearButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
答案 2 :(得分:0)
更好地使用UINavigationItem-iOS7Spacing类别