ios - Adding two UIBarButtomItems with image programmatically -
i trying use following code in viewdidload method add 2 uibarbuttonitems left side of navigation bar, second button shown:
//nav bar buttons uiview* leftbuttonview = [[uiview alloc]initwithframe:cgrectmake(0, 0, 40, 40)]; uibutton* leftbutton = [uibutton buttonwithtype:uibuttontypesystem]; leftbutton.backgroundcolor = [uicolor clearcolor]; leftbutton.frame = leftbuttonview.frame; [leftbutton setimage:[uiimage imagenamed:@"totop"] forstate:uicontrolstatenormal]; [leftbutton settitle:@"" forstate:uicontrolstatenormal]; leftbutton.tintcolor = [uicolor redcolor]; //your desired color. leftbutton.autoresizessubviews = yes; leftbutton.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleleftmargin; [leftbutton addtarget:self action:@selector(totop:) forcontrolevents:uicontroleventtouchupinside]; uiview* leftbuttonview2 = [[uiview alloc]initwithframe:cgrectmake(85, 0, 40, 40)]; uibutton* leftbutton2 = [uibutton buttonwithtype:uibuttontypesystem]; leftbutton2.backgroundcolor = [uicolor clearcolor]; leftbutton2.frame = leftbuttonview.frame; [leftbutton2 setimage:[uiimage imagenamed:@"tosectiontop"] forstate:uicontrolstatenormal]; [leftbutton2 settitle:@"" forstate:uicontrolstatenormal]; leftbutton2.tintcolor = [uicolor redcolor]; //your desired color. leftbutton2.autoresizessubviews = yes; leftbutton2.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleleftmargin; [leftbutton2 addtarget:self action:@selector(totop:) forcontrolevents:uicontroleventtouchupinside]; [leftbuttonview addsubview:leftbutton]; [leftbuttonview2 addsubview:leftbutton2]; uibarbuttonitem* leftbarbutton = [[uibarbuttonitem alloc]initwithcustomview:leftbuttonview]; uibarbuttonitem* leftbarbutton2 = [[uibarbuttonitem alloc]initwithcustomview:leftbuttonview2]; self.navigationitem.leftbarbuttonitem = leftbarbutton; self.navigationitem.leftbarbuttonitem = leftbarbutton2;
any welcome. thank you
you're setting same property twice second setting replaces first. try:
self.navigationitem.leftbarbuttonitems = @[ leftbarbutton, leftbarbutton2 ];
note setting leftbarbuttonitem*s*, takes array of bar buttons.
also, shouldn't need container views, add no value. buttons views can use them directly. setting frame origin of view may cause issues.
Comments
Post a Comment