UINavigationItem titleView position

前端 未结 7 748
长发绾君心
长发绾君心 2021-02-13 03:45

I\'m using UINavigationItem\'s titleView property to set a custom UILabel with my desired font size/color. Here\'s my code:

self.headerLabel = [[UILabel alloc] i         


        
7条回答
  •  闹比i
    闹比i (楼主)
    2021-02-13 03:57

    I have a same problem; I just somehow solved this issue by calculating the title length and set the label frame width accordingly. Although this is not a perfect one but can be manageable. Here is the code.

    label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor clearColor];
    label.font = [ UIFont fontWithName: @"XXII DIRTY-ARMY" size: 32.0 ];
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.0f];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor =[UIColor orangeColor];
    //label.text=categoryTitle;
    CGFloat verticalOffset = 2; 
    NSString *reqSysVer = @"5.0";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)           
    {
        if (categoryTitle.length > 8)
        {
        label.frame = CGRectMake(0, 0, 300, 44);
        }else {
            label.frame = CGRectMake(0, 0, 80, 44);
        }
    
        self.navigationItem.titleView = label;  
         self.navigationItem.title=label.text;
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset   forBarMetrics:UIBarMetricsDefault];
         [[UIBarButtonItem appearance] setTintColor:[UIColor newBrownLight]];
    
    
    }
    

提交回复
热议问题