ios - Navigationbar title alignment issue -


i have set custom view navigation bar titleview, when page first view controller title shown correctly in center

correct image

but when view controller pushed view controller title shifted right

incorrect image

code :

-(void)setuptwolinenavigationtitle {     cgfloat width = 0.95 * self.view.frame.size.width;     uiview *contentview = [[uiview alloc] initwithframe:cgrectmake(0, 0, width, 44)];     contentview.backgroundcolor = [uicolor clearcolor];      cgrect titlerect = contentview.frame;     titlerect.origin.y = 4;     titlerect.size.height = 20;      uilabel *titleview = [[uilabel alloc] initwithframe:titlerect];     titleview.backgroundcolor = [uicolor clearcolor];     titleview.font = [uifont systemfontofsize:14.0];     titleview.textalignment = nstextalignmentcenter;     titleview.textcolor = [uicolor blackcolor];     titleview.text = @"";     [contentview addsubview:titleview];      cgrect subtitlerect = contentview.frame;     subtitlerect.origin.y = 24;     subtitlerect.size.height = subtitlerect.size.height - 24;     //cgrect subtitleframe = cgrectmake(0, 24, 220, 44-24);     uilabel *subtitleview = [[uilabel alloc] initwithframe:subtitlerect];     subtitleview.backgroundcolor = [uicolor clearcolor];     subtitleview.font = [uifont systemfontofsize:11.0];     subtitleview.textalignment = nstextalignmentcenter;     subtitleview.textcolor = [uicolor blackcolor];     subtitleview.text = @"";     [contentview addsubview:subtitleview];      contentview.autoresizingmask = uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexiblerightmargin;     self.navigationitem.titleview = contentview; } 

can me in correcting ?

github link : https://github.com/iamabhiee/navigationtitledemo

here workaround, may useful, idea use uilabel titleview of navigationitem. way don't have manually calculate , adjust frame, automatically centered system. range in code hardcoded though.

// here create uilabel uilabel *label = [[uilabel alloc] initwithframe:cgrectzero]; label.backgroundcolor = [uicolor clearcolor]; label.textcolor = [uicolor blackcolor]; label.numberoflines = 0;  // set different font title , subtitle nsmutableattributedstring * string = [[nsmutableattributedstring alloc] initwithstring:@"title\nsubtitle"]; [string addattribute:nsfontattributename value:[uifont systemfontofsize:14.0] range:nsmakerange(0,5)]; [string addattribute:nsfontattributename value:[uifont systemfontofsize:11.0] range:nsmakerange(6,8)];  // set line spacing nsmutableparagraphstyle *paragrahstyle = [[nsmutableparagraphstyle alloc] init]; [paragrahstyle setlinespacing:6]; [paragrahstyle setalignment:nstextalignmentcenter]; [string addattribute:nsparagraphstyleattributename value:paragrahstyle range:nsmakerange(0, [string length])];  label.attributedtext = string; [label sizetofit];  self.navigationitem.titleview = label; 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -