iOS 导航栏按钮封装

- (UIBarButtonItem *)itemWithImageName:(NSString *)imageName highImageName:(NSString *)hightImageName action:(SEL)action
{
    UIButton *button = [[UIButton alloc]init];
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:hightImageName] forState:UIControlStateHighlighted];

    // 设置按钮的尺寸
    button.size = button.currentBackgroundImage.size;
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)tops
{
    NSLog(@"右面");
}

上面是封装好的导航栏按钮 直接调用就可以

self.navigationItem.rightBarButtonItem = [self itemWithImageName:@"wo" highImageName:@"" action:@selector(tops)];

你可能感兴趣的:(ios,导航栏按钮,barItem)