I hope to be button.selected

button.backgroundColor = [UIColor whiteColor];

Instead of black

- (void)viewDidLoad {
  UINavigationBar *  navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [rightButton setTitle:@"save" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor  blueColor] forState:UIControlStateNormal];
    [rightButton setBackgroundColor:[UIColor whiteColor]];
    [rightButton addTarget:self action:@selector(savetBtn:) forControlEvents:UIControlEventTouchUpInside];
    UINavigationItem *navItem = [[UINavigationItem alloc] init];
    UIBarButtonItem * buttonOfRight = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    [navItem setRightBarButtonItem :buttonOfRight animated:false];
    [navBar pushNavigationItem:navItem animated:false];
    [self.view addSubview:navBar];
}
-(void)savetBtn:(UIButton*)button{
    button.selected =! button.selected;

    if (button.selected) {

        [button setTitle:@"edit" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor whiteColor];

    }else{
        [button setTitle:@"save" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor whiteColor];
    }


}

After clicking the button, the background color is black, I hope it is white

0 Answers