2 exit door eXit_door 于 2013.08.12 10:58 提问

自定义 NavigationItem 超出范围

使用自定义 UIBarButtonItem 代替 leftBarButtonItem,但是当我如果按到按钮周围20像素的区域,也会执行按钮的动作。

代码:

- (void)changeNavbarButtons
{
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [backButton setBackgroundImage:[UIImage imageNamed:@"navbarBack.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -5;

    self.navigationItem.leftBarButtonItems = [NSArray
                                              arrayWithObjects:negativeSpacer, backItem, nil];    self.navigationItem.hidesBackButton = YES;
}

negativeSpacer 向左移动了按钮5个像素。

Csdn user default icon
上传中...
上传图片
插入图片