我在导航栏的自定义右侧栏按钮项中添加了一个自定义按钮,如图所示。
我希望能够删除按钮后的空间,使其接触屏幕的右边缘,但即使在搜索后也找不到解决方案。如果有人能提到如何,那就太好了。
这是我正在使用的代码
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(50.0, 0.0, 60, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(testButtonControl:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = aBarButtonItem;
最佳答案
你可以添加一个负宽度的固定空间元素(我知道,这听起来像一个黑客,但它有效):
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(50.0, 0.0, 60, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(testButtonControl:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *spaceFix = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
spaceFix.width = -5;
self.navigationItem.rightBarButtonItems = @[spaceFix, aBarButtonItem];
关于xcode - 如何删除 UIBarButtonItem 旁边的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11814983/