How to center the image in navigation across all UIViewControllers? Swift / obj-c

The problem is visual:

The logo is centered in empty space, not in the navigationBar

I tried to put the image in the center of my frame with no luck. I also tried to focus it on the game x CGRect. I suppose I can just put a blank icon with the same background as the navigation bar; however, I do not want to do this. I can have 2-3 icons on the right; then what?

  let image = UIImage(named: "some_logo")! let imageSize = CGSizeMake(60, 42) let marginX: CGFloat = (self.navigationController!.navigationBar.frame.size.width / 2) - (imageSize.width / 2) let imageView = UIImageView(frame: CGRect(x: marginX, y: 0, width: imageSize.width, height: imageSize.height)) imageView.image = image imageView.contentMode = .ScaleAspectFit self.navigationItem.titleView = imageView 

I prefer fast, but obj-c solutions are welcome as well.

Any pointers appreciated.

This app has nothing to do with KIA, it’s just some logo that I got from a Google search looking for “some logo”.

+5
source share
5 answers

I ran into the same problem. Then I tried one code shown below.

  override func viewDidAppear(animated: Bool) { let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 40)) imageView.contentMode = .ScaleAspectFit let image = UIImage(named: "googlePlus") imageView.image = image navigationItem.titleView = imageView } 

This code works fine when I tested the Left and Right buttons.

But in my previous code the right panel is missing.

So, the image moves to the right.

To solve this, I created a button on the right panel and changed the hue color to clear the color.

So everything is working fine. This is one workaround for your problem.

+7
source

The easiest way to do this is in Interface Builder.

Just drag and drop the “NavigationItem” from the object library and place it in your ViewController, then put the UIView where the title is the title (make sure the background is “cleaned”) Then put the UIImageView in that view and set the image in the Attributes Inspector to the desired image. Scale your UIImage accordingly and set your limits accordingly.

+4
source

The question title is “Swift / Obj-C”, so I use the Obj-C code:

 UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView; titleImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2) - (100/2), 0, 100,self.navigationController.navigationBar.frame.size.height)]; //setting the image for UIImageView titleImage.image = [UIImage imageNamed:@"someLogo"]; titleImage.contentMode = UIViewContentModeCenter; self.navigationItem.titleView = titleImage; 
+2
source

How about setting the center of your image to the navigationBar.center plan instead of setting the field?

 //assuming we already have our navigationController let myNicelLogoWidth = 100 let myNiceLogoHeight = 50 //start positioning your logo at 0.0, 0.0 let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: myNicelLogoWidth, height: myNiceLogoHeight)) imageView.contentMode = .scaleAspectFit imageView.center = navigationBar.center //the put your image at the center let image = UIImage(named: "myNiceLogoImage") imageView.image = image navigationItem.titleView = imageView 
+1
source

I ran into this problem, and finally, I found out that the problem is in the previous navigation bar title, which is still next to the hamburger button, but it is invisible.

A quick fix, but not sure if it's best to change the previous title of the navigation bar to an empty line before displaying the next view controller.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1240055/


All Articles