0

I am new to iOS development but not so new to C#. I chose to do the tutorial provided by Xamarin/Microsoft and then to try and expand on that. I was looking for a way to set an image in the center of the UINavigationBar, and I found that the proper way to do so was to use NavigationItem.TitleView = myImageView;

This works great! However, there is one issue: the image I used is simply bigger than the NavigationBar. I want to scale (and keep the aspect ratio) the image to fit inside of the NavigationBar while also looking nice. Here is what I've done up to this point to get the image:

var logo = UIImage.FromFile("PolarisLogoBlank.png");
var iView = new UIImageView(logo);

NavigationItem.TitleView = iView;

And here is a picture of what I'm getting:

enter image description here

Any help is much appreciated; thanks SO!

Trevor Yokum
  • 152
  • 1
  • 10

2 Answers2

4

You have to set both the height of the imageView and the contentMode of the imageView so that the image is not stretched. This is what you are looking for:

iView.contentMode = .scaleAspectFit
if let navigationController = navigationController  {
    iView.frame.size.height = navigationController.navigationBar.frame.size.height
}

or with force unwrapping

iView.contentMode = .scaleAspectFit
iView.frame.size.height = navigationController!.navigationBar.frame.size.height
Darryl Johnson
  • 431
  • 3
  • 10
  • 1
    So, the `iView.ContentMode = UIViewContentMode.ScaleAspectFit;` works just fine; however, when I try `iView.Frame.Size.Height = NavigationController.NavigationBar.Frame.Size.Height;`, it tells me that I "cannot modify the return value of `CGRect.Size` because it is not a variable" – Trevor Yokum Jan 27 '17 at 21:19
  • Ahh yes. This `iView.frame.size.height =` in swift is a shortcut that actually creates a new struct (CGSize) with the old width value and the new height value and then assigns it to `iView.frame.size`. So you have to actually create a new Size struct and assign the width to the old width and the height to the new height. CGRect (the frame) is also a struct so you may actually need to create a new Rect and assign it to `iView.frame`. – Darryl Johnson Jan 27 '17 at 21:23
  • I have tested @DarrylLJohnson 's code and it works fine. – thecloud_of_unknowing Jan 27 '17 at 21:27
  • Take a look at http://stackoverflow.com/questions/1747654/cannot-modify-the-return-value-error-c-sharp – Darryl Johnson Jan 27 '17 at 21:33
  • @DarrylLJohnson I appreciate it brother! I actually ended up doing this: `var newRect = new CGRect();` -> `newRect = iView.Frame;` -> `newRect.Height = NavigationController.NavigationBar.Frame.Size.Height;` -> `newRect.Width = iView.Frame.Size.Width;` -> `iView.Frame = newRect;` – Trevor Yokum Jan 27 '17 at 21:47
  • Yeah. That's exactly what you needed to do. – Darryl Johnson Jan 27 '17 at 21:51
1

You can set the size of the imageView:

iView.frame.size.height = self.navigationController.navigationBar.frame.size.height;

I'm not sure what exactly that looks like in C#, but that's how you'd do it in Objective-C.

Hope that helps.

naturaln0va
  • 755
  • 6
  • 8
  • You were on the exact same track that Darryl above was, definitely tossed you an upvote. What you posted includes shortcuts in Swift that don't exist in C#, so I had to make a new Frame (CGRect) struct and manipulate the values. Thanks dude! – Trevor Yokum Jan 27 '17 at 21:55
  • No problem, sorry I couldn't provide the whole answer. Glad you got it figured out! – naturaln0va Jan 27 '17 at 22:04