How to put an image in the center of navigationBar of a UIViewController?

后端 未结 7 2233
耶瑟儿~
耶瑟儿~ 2021-02-01 05:31

I have this snippet of code used in viewDidLoad of a UIViewController. I\'va no errors. Images exists. I get the background but not the image. Image is a sort of logo.



        
7条回答
  •  不要未来只要你来
    2021-02-01 06:03

    First we have to create a view which have size as same as navigation bar then add an image view and set set its frame as it looks center in the navigation bar.It works for all ios version and it automatically takes frame size as per device (retina or normal) and works like magic.

    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, 320, 44);
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Header.png"]];
    imgView.frame = CGRectMake(75, 0, 150, 44);
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    
    [headerView addSubview:imgView];
    
    navigationCtrl.navigationBar.topItem.titleView = headerView;
    
    [headerView release];
    [imgView release];
    

提交回复
热议问题