Как удалить NavigationBar Right Space? - PullRequest
0 голосов
/ 01 мая 2020

Я играю с кнопками панели навигации. Мне нужна кнопка с правого края с пробелом 0, после поиска в Google я обнаружил код ниже

 self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:@"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:@selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:self.filterButton];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -25;
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];

Но, к сожалению, в iOS 13 негативное пространство не работает. Кто-нибудь может предложить мне решения.

Прикрепленный скриншот здесь enter image description here Спасибо.

1 Ответ

0 голосов
/ 01 мая 2020

Давайте попробуем этот простой способ. Давайте определим ширину пространства BarButtonItem равной ширине self.view.frame.size.width - width of the button.

self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:@"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:@selector(filterAction:) 
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] 
initWithCustomView:self.filterButton];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil 
action:nil];
negativeSpacer.width = self.view.frame.size.width - 45; // As 45 is the frame width of your filter button 
[self.navigationItem setRightBarButtonItems:[NSArray 
arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];

Надеюсь, это поможет!

...