How to change the border color below the navigation bar?

前端 未结 6 1886
甜味超标
甜味超标 2020-12-28 19:40

Can anyone advise me on how the border below the navigation bar can be changed?

\"enter

6条回答
  •  伪装坚强ぢ
    2020-12-28 20:27

    Despite navigationBar is read-only property for UINavigationController you can avoid this restriction by "setValue:forKey:". This method was approved on 5 applications successfully submitted to AppStore.

    You can subclass UINavigationBar and change drawRect: method as you want. For example,

    @implementation CustomNavigationBar
    
    - (void) drawRect:(CGRect)rect
    {
        [super drawRect:rect];
        UIImage *backgroundImage = ImageFromColor(WANTED_COLOR);
        [backgroundImage drawInRect:rect];
    }
    

    After you can subclass UINavigationController and change initWithRootViewController:

    - (id) initWithRootViewController:(UIViewController *)rootViewController
    {
        self = [super initWithRootViewController:rootViewController]; 
        if (self) 
        {
            CustomNavigationBar *navBar = [CustomNavigationBar new];
            [self setValue:navBar forKey:@"navigationBar"];
        }
        return self;
    }
    

    Also you can vary this approach by making Category for UINavigationController and implementing method swizzling for initWithRootViewController:

    P.S. Yesterday my new app appeared at AppStore without any problem with this approach.

提交回复
热议问题