ios - UIButton Image is not changed in Hightlight or Selected state -
i wrote following code of button ios8 app:
uibutton *btnback = [uibutton buttonwithtype:uibuttontypecustom]; [btnback setframe:cgrectmake(40, 30, 30, 30)]; [btnback setbackgroundimage:[uiimage imagenamed:@"btn_close.png"]forstate:uicontrolstatenormal]; [btnback setbackgroundimage:[uiimage imagenamed:@"btn_close_on.png"] forstate:uicontrolstatehighlighted|uicontrolstateselected]; [btnback addtarget:self action:@selector(btnbackclick:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btnback];
and method when click:
-(void) btnbackclick:(id)sender{ [self.navigationcontroller poptorootviewcontrolleranimated:yes]; }
however, background of image not change when button being click. effect grayout.
if draw button on xib file, image change works charm.
anything wrong? please help.
uibutton *btnback = [uibutton buttonwithtype:uibuttontypecustom]; [btnback setframe:cgrectmake(40, 30, 30, 30)]; [btnback setbackgroundimage:[uiimage imagenamed:@"btn_close.png"]forstate:uicontrolstatenormal]; [btnback setbackgroundimage:[uiimage imagenamed:@"btn_close_on.png"] forstate:uicontrolstatehighlighted|uicontrolstateselected]; [btnback addtarget:self action:@selector(btnbackclick:) forcontrolevents:uicontroleventtouchupinside]; [btnback setselected:yes]; [self.view addsubview:btnback];
//**********updated*******
uibutton *btnback = [uibutton buttonwithtype:uibuttontypecustom]; [btnback setframe:cgrectmake(40, 30, 30, 30)]; [btnback setbackgroundimage:[uiimage imagenamed:@"btn_close.png"]forstate:uicontrolstatenormal]; [btnback setbackgroundimage:[uiimage imagenamed:@"btn_close_on.png"] forstate:uicontrolstatehighlighted]; [btnback setbackgroundimage:[uiimage imagenamed:@"btn_close_on.png"] forstate:uicontrolstateselected]; [btnback addtarget:self action:@selector(btnbackclick:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btnback]; -(void)btnbackclick:(uibutton *)btn { if ([btn isselected]) { [btn setselected:no]; } else [btn setselected:yes]; }
Comments
Post a Comment