我希望我的自定义按钮在导航栏中具有白色背景颜色,而不是黑色

时间:2019-04-27 17:15:51

标签: ios button

我希望成为button.selected

button.backgroundColor = [UIColor whiteColor];

代替黑色

- (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];
    }


}

点击按钮后,背景颜色为黑色,希望是白色

0 个答案:

没有答案