iOS navigationItem图片显示在中间或者平铺的问题

有时候放在 navigationItem 上的图片尺寸太大,会造成图片看上去就像放在了 navigationItem 的中间,或者平铺到了 navigationItem 上,这是就要调整 navigationItem button 的大小

//创建按钮
UIButton *rightCustomButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
//给按钮添加图片
[rightCustomButton setBackgroundImage:[UIImage imageNamed:@"sharewx"] forState:UIControlStateNormal];
//创建按钮的父视图,大小要和按钮的大小相同
UIView *rightCustomView = [[UIView alloc] initWithFrame: rightCustomButton.frame];
//把按钮添加到父视图上
[rightCustomView addSubview: rightCustomButton];
//创建item
UIBarButtonItem * rightButtonItem =[[UIBarButtonItem alloc] initWithCustomView: rightCustomView];
//给按钮添加事件
[rightCustomButton addTarget:self action:@selector(rightBarButtonItemActionShare) forControlEvents:UIControlEventTouchUpInside];
//设置按钮
self.navigationItem.rightBarButtonItem = rightButtonItem;

你可能感兴趣的:(iOS navigationItem图片显示在中间或者平铺的问题)