简体   繁体   中英

How to deal with default paddings while customise navigation bar with title view?

I am trying to customise the navigation bar with title view. But it seems setting title view comes with its own left and right and top paddings.I was expecting the title view to cover the whole navigation bar according to the frames given.

Is it expected behaviour and if YES than how to deal with that?

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];
    view.backgroundColor = [UIColor greenColor];
    //Navigation Bar
    self.navigationItem.titleView = view;

导航标题视图为绿色

If you just want the navigation bar to be green use [self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]]; in iOS 7+ or [self.navigationController.navigationBar setTintColor:[UIColor greenColor]]; in iOS 6-

Yes, it seems it is not possible to remove that left/right padding. This is the screenshot form debugging views in Xcode after running your code

在此处输入图片说明

The outside view that is gray is the navigationBar and the green is obviously the titleView . No matter the frame for the titleView , it is clipped.

Apple documentation says just this:

Custom title views are centered on the navigation bar and may be resized to fit.

I think the only solution would be to subclass the navigationBar , so that you override the titleView frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM