由于系统的UIBarButtonItem *rightButton和UIBarButtonItem *rightButton的位置已经固定,但有时我们又想改变他的位置,可以这么做
// 设置右边的按钮
UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
[rightBtn setImage:[UIImage imageNamed:@"white_Cart"] forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -15.0; // 这样rightButton就会向右移动15,也就是更接近边框了
self.navigationItem.rightBarButtonItems = @[negativeSpacer, rightButton];
时间: 2024-10-11 12:27:00