18

因此,在使用 UIButton 作为自定义视图创建 UIBarButtonItem 时,我遇到了许多其他人遇到的相同问题。

基本上,该按钮距左侧或右侧大约 10 个像素。当我使用没有自定义视图的常规 BarButtonItem 时,不会发生这种情况。

这篇文章提供了部分解决方案: UIBarButton With Custom View

这是我通过子类化 UIButton 创建的代码(如另一篇文章中所述)

    - (UIEdgeInsets)alignmentRectInsets {
    UIEdgeInsets insets;
    if ([self isLeftButton]) {
        insets = UIEdgeInsetsMake(0, 9.0f, 0, 0);
    }
    else { // IF ITS A RIGHT BUTTON
        insets = UIEdgeInsetsMake(0, 0, 0, 9.0f);
    }
    return insets;
}


- (BOOL)isLeftButton {
    return self.frame.origin.x < (self.superview.frame.size.width / 2);
}

这很好用,但是当我从导航控制器弹出一个视图控制器回到这个主视图时,按钮仍然错误定位大约 0.3 秒,然后它卡入正确的插图。

这是一个巨大的眼睛,我不知道如何阻止它像这样折断。有什么想法吗?谢谢!

4

4 回答 4

53

我和你和其他许多人一样有同样的问题。经过长时间的尝试修复它,我终于做到了。这是您必须包含在 *-Prefix.pch 文件中的类别。就这样!

UINavigationItem+iOS7Spacing.h

#import <Foundation/Foundation.h>
@interface UINavigationItem (iOS7Spacing)
@end

UINavigationItem+iOS7Spacing.m

#import "UINavigationItem+iOS7Spacing.h"
#import <objc/runtime.h>

@implementation UINavigationItem (iOS7Spacing)

- (BOOL)isIOS7
{
    return ([[[UIDevice currentDevice] systemVersion] compare:@"7" options:NSNumericSearch] != NSOrderedAscending);
}

- (UIBarButtonItem *)spacer
{
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -11;
    return space;
}

- (void)mk_setLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem
{
    if ([self isIOS7] && leftBarButtonItem) {
        [self mk_setLeftBarButtonItem:nil];
        [self mk_setLeftBarButtonItems:@[[self spacer], leftBarButtonItem]];
    } else {
        [self mk_setLeftBarButtonItem:leftBarButtonItem];
    }
}

- (void)mk_setLeftBarButtonItems:(NSArray *)leftBarButtonItems
{
    if ([self isIOS7] && leftBarButtonItems && leftBarButtonItems.count > 0) {

        NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:leftBarButtonItems.count + 1];
        [items addObject:[self spacer]];
        [items addObjectsFromArray:leftBarButtonItems];

        [self mk_setLeftBarButtonItems:items];
    } else {
        [self mk_setLeftBarButtonItems:leftBarButtonItems];
    }
}

- (void)mk_setRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem
{
    if ([self isIOS7] && rightBarButtonItem) {
        [self mk_setRightBarButtonItem:nil];
        [self mk_setRightBarButtonItems:@[[self spacer], rightBarButtonItem]];
    } else {
        [self mk_setRightBarButtonItem:rightBarButtonItem];
    }
}

- (void)mk_setRightBarButtonItems:(NSArray *)rightBarButtonItems
{
    if ([self isIOS7] && rightBarButtonItems && rightBarButtonItems.count > 0) {

        NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:rightBarButtonItems.count + 1];
        [items addObject:[self spacer]];
        [items addObjectsFromArray:rightBarButtonItems];

        [self mk_setRightBarButtonItems:items];
    } else {
        [self mk_setRightBarButtonItems:rightBarButtonItems];
    }
}

+ (void)mk_swizzle:(SEL)aSelector
{
    SEL bSelector = NSSelectorFromString([NSString stringWithFormat:@"mk_%@", NSStringFromSelector(aSelector)]);

    Method m1 = class_getInstanceMethod(self, aSelector);
    Method m2 = class_getInstanceMethod(self, bSelector);

    method_exchangeImplementations(m1, m2);
}

+ (void)load
{
    [self mk_swizzle:@selector(setLeftBarButtonItem:)];
    [self mk_swizzle:@selector(setLeftBarButtonItems:)];
    [self mk_swizzle:@selector(setRightBarButtonItem:)];
    [self mk_swizzle:@selector(setRightBarButtonItems:)];
}

@end

GitHub 上的 UINavigationItem+iOS7Spacing 分类

于 2013-10-11T11:31:13.140 回答
4

UIButton不是Marius 的回答中的子类化或方法混搭的超级粉丝: https ://stackoverflow.com/a/19317105/287403

我只是使用了一个简单的包装器,并将按钮框架的 x 向负方向移动,直到找到正确的位置。按钮点击似乎也很好(尽管如果需要,您可以扩展按钮的宽度以匹配负偏移量)。

这是我用来生成新后退按钮的代码:

- (UIBarButtonItem *) newBackButton {
    // a macro for the weak strong dance
    @weakify(self);
    UIButton *backButton = [[UIButton alloc] init];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:17];
    CGFloat width = [@"Back" boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil].size.width + 27;
    backButton.frame = CGRectMake(-17.5, 0, width + 17.5, 44);
    [backButton setImage:[UIImage imageNamed:@"UINavigationBarBackIndicatorDefault"] forState:UIControlStateNormal];
    [backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 14, 0, 0)];
    [backButton setTitleColor:mTCOrangeColor forState:UIControlStateNormal];
    backButton.contentEdgeInsets = UIEdgeInsetsZero;
    backButton.imageEdgeInsets = UIEdgeInsetsZero;
    [backButton addEventHandler:^(id sender) {
        // a macro for the weak strong dance
        @strongify(self);
        // do stuff here
    } forControlEvents:UIControlEventTouchUpInside];
    UIView *buttonWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
    [buttonWrapper addSubview:backButton];
    return [[UIBarButtonItem alloc] initWithCustomView:buttonWrapper];
}
于 2014-01-09T19:57:53.857 回答
0

或者,您可以调整自定义视图内容的框架(左侧子视图为-5,右侧子视图为+5),只要您的自定义视图没有clipsToBounds设置为,YES那么这些内容将被尊重。这不是最干净的,但其他建议的解决方案可能更 hacky IMO。

子视图在 IB 中会出现 5 个点的截断,但是当您在模拟器中运行应用程序时会显示全部内容。

于 2014-06-25T22:16:19.753 回答
0

我没有为多个 navigationBarButton 项目尝试过这段代码,但它似乎适用于单个按钮。我已经覆盖了 UINavigationBar 及其 layoutSubviews 方法。

首先,在文件顶部定义了一个水平偏移常数。修改如你所愿:

static CGFloat const kNavBarButtonHorizontalOffset = 10;

布局子视图:

- (void)layoutSubviews{

    [super layoutSubviews];

    //Do nothing on iOS6
    if ( ![[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f){return;}

    UINavigationItem * navigationItem = [self topItem];

    for(UIBarButtonItem * item in [navigationItem rightBarButtonItems]){

        UIView * subview = [item customView];
        CGFloat width = CGRectGetWidth(subview.frame);

        CGRect newRightButtonRect = CGRectMake(CGRectGetWidth(self.frame) - width - kNavBarButtonHorizontalOffset,
                                           CGRectGetMinY(subview.frame),
                                           width,
                                           CGRectGetHeight(subview.frame));
        [subview setFrame:newRightButtonRect];

    }

    for(UIBarButtonItem * item in [navigationItem leftBarButtonItems]){
        UIView * subview = [item customView];
        CGRect newRightButtonRect = CGRectMake(kNavBarButtonHorizontalOffset,
                                           CGRectGetMinY(subview.frame),
                                           CGRectGetWidth(subview.frame),
                                           CGRectGetHeight(subview.frame));

        [subview setFrame:newRightButtonRect];
    }
}
于 2014-03-25T15:07:49.563 回答