UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame= CGRectMake(0, 0, 40, 44);
[btn addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:btn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
/**
width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和 边界间距为5pix,所以width设为-5时,间距正好调整为0;width为正数 时,正好相反,相当于往左移动width数值个像素
*/
negativeSpacer.width = -5;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
千万不要设置tintcolor
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame= CGRectMake(0, 0, 30, 44);
[btn setImage:[[UIImage imageNamed:@"search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *itemRight = [[UIBarButtonItem alloc] initWithCustomView:btn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.rightBarButtonItems = @[negativeSpacer,itemRight];
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。