我已将自定义按钮添加到导航栏的自定义右栏按钮项中,如图所示。
我希望能够删除按钮后的空间,以使其触摸屏幕的右边缘,但是即使搜索后也找不到解决方案。如果有人可以提及如何,那将是很棒的。
这是我正在使用的代码
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;
最佳答案
您可以添加一个宽度为负的固定空格元素(我知道,这听起来像是hack,但是可以用):
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/