0

我使用itiWithCustomView方法,其中customViewUIButtoncontentHorizontalAlignmentUIControlContentHorizontalAlignmentRight創建UIBarButtonItemrightBarButtonItem
barbuttonitem工作正常。
但推動pop viewcontroller操作後,barbuttonitem點擊面積大幅減少(我認爲不超過3-4個像素)。
請幫我這個問題的UIBarButtonItem用的UIButton爲CustomView奇怪的問題

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; 
+0

請添加至少一個剪斷代碼。 –

+0

這裏完全沒有太多的信息。 –

+0

如果我在viewDidLoad問題中添加按鈕,如果我在viewWillAppear中添加代碼,那麼沒問題。這種模糊行爲的任何理由? –

回答

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; 
} 
+0

感謝這個奇妙的建議,但看到我的問題中的評論。你的解決方案不起作用 –