ios - How to subclass UINavigationBar correctly also using appearance -


this what trying achieve: navigationbar nb: title controller.

some additional information:-

  1. this navigation bar appear on around 10 view controllers.
  2. it not part of navigation controller, not required be.
  3. the button on left static (will on navigation bar).
  4. the right button not static , either not exist or different on different view controllers.
  5. the left button acts sliding menu (i.e. menu slides in underneath left)
  6. i using storyboard in setup

question correct way achieve this?

existing code have tried implement this, can 1 or other of buttons, without title working. implement background used appearance api below:

didfinishlaunching...

// custom navigation bar appearance setup [[uinavigationbar appearance] settitletextattributes: [nsdictionary dictionarywithobjectsandkeys:                                                        [uicolor colorwithred:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], uitextattributetextcolor,                                                        [uicolor colorwithred:0.0 green:0.0 blue:0.0 alpha:0.5],uitextattributetextshadowcolor,                                                        [nsvalue valuewithuioffset:uioffsetmake(0, 1)],                                                        uitextattributetextshadowoffset,nil]]; // used deal rotation of nav bar when implemented outside of navigation controller [[uinavigationbar appearance] setautoresizingmask:uiviewautoresizingflexiblewidth | uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexibleheight]; [[uinavigationbar appearance] setcontentmode:uiviewcontentmodescaleaspectfit];  // set background image uiimage *imagebg = [[uiimage imagenamed: @"mainnavbar"] resizableimagewithcapinsets:uiedgeinsetsmake(0,5,0,5)]; [[uinavigationbar appearance] setbackgroundimage:imagebg forbarmetrics:uibarmetricsdefault];  // used push title down when in landscape , navbar outside of navigation controller [[uinavigationbar appearance] settitleverticalpositionadjustment:2 forbarmetrics:uibarmetricslandscapephone]; 

i subclassed uinavigationbar left button creation following: subclass of uinavigationbar: awakefromnib

uinavigationitem* ni = [[uinavigationitem alloc] init]; uibutton *leftbutton = [[uibutton alloc] initwithframe:cgrectmake(0, 0, 38.0f, 29.0f)]; [leftbutton setimage:[uiimage imagenamed:@"menubaritem"] forstate:uicontrolstatenormal]; [leftbutton addtarget:nil action:@selector(menuitempressed:) forcontrolevents:uicontroleventtouchupinside]; [leftbutton setcontentmode:uiviewcontentmodescaleaspectfit]; [leftbutton setautoresizingmask:uiviewautoresizingflexibleheight | uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexibletopmargin]; uibarbuttonitem *b =[[uibarbuttonitem alloc] initwithcustomview:leftbutton]; ni.leftbarbuttonitem = b; self.items = @[ni]; 

then within storyboard add navigationbar vc in question , link correct subclass. add additional button item navigation bar in storyboard controller , link outlet. within vc try update title , right button below:

view controller holds subclassed nav bar: viewdidload

// set vc title navbar self.title  = nslocalizedstring(@"navbarhometitle", @"navigation bar - home page title");  // setup right navigation bar item uiimage *addimage = [uiimage imagenamed:@"addgamebutton"]; uibutton *addbutton = [uibutton buttonwithtype:uibuttontypecustom]; [addbutton setframe:cgrectmake(0, 0, addimage.size.width, addimage.size.height)]; [addbutton setbackgroundimage:addimage forstate:uicontrolstatenormal]; [addbutton addtarget:self action:@selector(addgamebuttonpressed:) forcontrolevents:uicontroleventtouchupinside]; [self.rightbarbuttonitem setcustomview:addbutton]; 

the result navigation bar left button. believe due awakefromnib code above alloc init new uinavigationitem therefore cannot control title , add button it. how can complete this? best approach or there others cannot see in-depth tutorials on how approach this.

your better option use navigation controller, acts controller interrogates view controller it's displaying , updates navigation bar.

rather subclass navigation bar, should subclass navigation controller. whenever displayed view controller changes can set left button item adding navigation item of view controller. each view controller can specify whatever right bar button item(s) requires.

the simplest way navigation controller act own delegate , implement navigationcontroller:willshowviewcontroller:. using method can access navigationitem of passed view controller , set leftbarbuttonitem. have access other methods of navigation , view controllers if required.

each view controller also, in viewdidload, set self.navigationitem.rightbarbuttonitem(s) appropriate buttons requirements of view. such 'addgamebutton' bar button.

working way, appearance work have it, title of view controller work automatically , have full control, , number if methods in navigation controller subclass, perform button , interaction customisations.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -