ios:点击一个button不断切换背景图片

1.UINavigationItem上怎么放UIButton 2.根据button的不同点击状态可以在两个背景图片之间来回切换。
  UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(self.view.frame.size.width - 40, 0, 40, 30);
    [rightButton setBackgroundImage:[UIImage imageNamed:@"btn_nav_collection@2x"] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"btn_nav_list@2x"] forState:UIControlStateSelected];
    [rightButton addTarget:self action:@selector(rightBarButtonItemClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

- (void) rightBarButtonItemClicked:(UIButton *)sender{
    sender.selected = !sender.selected;
}


你可能感兴趣的:(编程小技巧)