UIBarButtonItem与UIButton作为CustomView奇怪的问题

时间:2016-02-27 10:26:43

标签: ios objective-c uinavigationcontroller uibutton uibarbuttonitem

我使用rightBarButtonItem方法创建itiWithCustomViewcustomView,其中UIButtoncontentHorizontalAlignment UIControlContentHorizontalAlignmentRightbarbuttonitemviewcontroller }。
barbuttonitem工作正常 但在推送弹出UIImage *filterImage = [UIImage imageNamed:@"filter"]; UIButton *filterButton = [UIButton buttonWithType:UIButtonTypeCustom]; filterButton.frame = CGRectMake(0, 0, 44, 44); filterButton.showsTouchWhenHighlighted = YES; [filterButton setImage:[UIImage imageNamed:@"filter"] forState:UIControlStateNormal]; [filterButton addTarget:self action:@selector(filterTapped:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *filterItem = [[UIBarButtonItem alloc]initWithCustomView:filterButton]; self.navigationItem.rightBarButtonItem = filterItem; 操作后,function balance(arr) { return arr.reduce(function(sum,item) { return sum + (item instanceof Array ? balance(item) : item); }, 0); }; balance([1,[2,1,[1,2,-1],[1]],1,[2,1]]); // 11 点击区域会大大减少(我认为不超过3-4像素)。
请帮我解决这个问题

Number.prototype.balance = function() { return this; };
Array.prototype.balance = function() { return this.reduce(function(a,b) { return a + b.balance(); }, 0); }

[1,[2,1,[1,2,-1],[1]],1,[2,1]].balance(); // 11

1 个答案:

答案 0 :(得分:0)

我认为您将移动条形按钮的对齐分配到右侧。除此之外,您可以尝试以下代码段。它可能会解决您的问题。

self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.rightBarButtonItems = [self setRightBarButtonWithTarget:self action:@selector(topRightButtonClick) image:@"imagename"];

创建创建右键按钮的常用方法

- (NSArray *)setRightBarButtonWithTarget:(id)target action:(SEL)action image:(NSString *)imgName
{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.backgroundColor = [UIColor clearColor];
    [rightBtn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [rightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = CGRectMake(0, 0, 26, 26);

    UIBarButtonItem *negativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpace.width = -5;
    UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];

    NSArray *arrBarItems = [NSArray arrayWithObjects:negativeSpace, homeButton, nil];
    return arrBarItems;
}