How to place a UIImage in Navigationbar such that its a logo?

后端 未结 8 733
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 07:30

\"enter\"enterI

相关标签:
8条回答
  • 2020-12-22 08:07

    What I usually do is

    1. Hide Navigation Bar
       [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    2. Use image of size 640 x 88 & 320 x 44
       UIImage *navigationImage = [UIImage imageNamed:@"navigationImage.png"];
    
    3. Use this image in Imageview at frame CGRectMake(0,0,320,44)
       UIImageView *navImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,44)];
       navImageView.image = navigationImage;
       [self.view addSubview:navImageView];
    
    0 讨论(0)
  • 2020-12-22 08:08

    I have tried and got the below solution,

    func addIconToNavigationBar(){
        let logoContainer = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
    
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
        imageView.contentMode = .scaleAspectFit
        let image = UIImage(named: "FlashScreenTitle")
        imageView.image = image
        logoContainer.addSubview(imageView)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: logoContainer)
    }
    

    In the above solution, I have Created a UIView and added ImageView inside that View. Then Created custom BarButtonItem, with my UIView and assigned it to the leftBarButtonItem.

    Remember to call addIconToNavigationBar() in viewDidLoad() method

    0 讨论(0)
  • 2020-12-22 08:12
    UIBarButtonItem *logoItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
    UIBarButtonItem *logoItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItem target:self action:nil];
    
    NSArray *actionButtonItems = @[logoItem1, logoItem2];
    self.navigationItem.rightBarButtonItems = actionButtonItems;
    
    0 讨论(0)
  • 2020-12-22 08:13

    Try it like this:

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
    

    Also make sure UIImage is not nil and it does have a valid image.

    0 讨论(0)
  • 2020-12-22 08:14

    Insert the Bar Button Item into your Viewcontroller on left Side, and insert the image in Bar item see the image

    enter image description here

    0 讨论(0)
  • 2020-12-22 08:22

    I think this is the problem with image rendering mode. Can you try the below code

        UIImage *logo = [[UIImage imageNamed:@"logo"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:logo style:UIBarButtonItemStylePlain target:self action:nil];
        self.navigationItem.leftBarButtonItem = item;
    

    You can even try with your current code by setting the UIImageRenderingModeAlwaysOriginal.

    0 讨论(0)
提交回复
热议问题