È possibile utilizzare un UIButton
come customView
di un UIBarButtonItem
e hanno due immagini di sfondo diverse per a normal
e selected
stato di UIButton. Quindi, quando si tocca il pulsante, è possibile impostare lo stato selected
su YES
o NO
.
// Build right bar button
UIImage *normalButtonImage = [UIImage imageNamed:@"normal-button"];
UIImage *selectedButtonImage = [UIImage imageNamed:@"selected-button"];
CGRect rightButtonFrame = CGRectMake(0, 0, normalButtonImage.size.width,
normalButtonImage.size.height);
UIButton *rightButton = [[UIButton alloc] initWithFrame:rightButtonFrame];
[rightButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:selectedButtonImage forState:UIControlStateSelected];
[rightButton addTarget:self action:@selector(rightBarButtonPress)
forControlEvents:UIControlEventTouchDown];
[orientationButton setShowsTouchWhenHighlighted:YES];
[orientationButton setSelected:NO];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[self.navigationItem setRightBarButtonItem:rightBarButton];
Allora il tuo metodo per quando il pulsante viene premuto, modificare lo stato
- (void)rightBarButtonPress
{
//toggle selected state of button
UIBarButtonItem *rightBarButton = self.navigationItem.rightBarButtonItem;
UIButton *button = (UIButton *)rightBarButton.customView;
[button setSelected:!button.selected];
//do whatever else you gotta do
}
fonte
2015-05-22 19:38:51
Mi chiedo anche se ci sia modo di farlo: la vista personalizzata con UIButton è la soluzione più vicina che io conosca, ma non può gestire metriche della barra diverse.Io –
è giusto. attraverso qualsiasi soluzione possibile per questo comportamento desiderato. –