3

I'm working on some screen in which at top I have an application logo: I want this logo to be resized based on the free size it has. So I configured it with the desired constraints and when it has enough space it like the desired result:

enter image description here

The problem starts when the UIImageView does not have enough space to spread in this case the image is centred in the UIImageView and leaves an empty space above it:

enter image description here

Now I tried to place the UIImageView inside a simple UIView and place it accordingly inside, but so far without result.

I also tried to search for an answer here but all the solutions I have found:

how to set image in top avoiding space in UIimageView

Alignment UIImageView with Aspect Fit

and a custom view:

https://github.com/reydanro/UIImageViewAligned

were in Objective-C when I'm using Swift. How can I achieve this using the Storyboard on Swift?

halfer
  • 19,824
  • 17
  • 99
  • 186
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • Are you using AutoLayout ? – technerd Jan 21 '16 at 12:55
  • @technerd No, I'm using layout constraints. – Emil Adz Jan 21 '16 at 12:57
  • You can add an `UIIMageView` within a container `UIView`which represents the available space for the logo, and set a ratio constraint for the image view equal to the same ratio as the image one. – Michaël Azevedo Jan 21 '16 at 12:58
  • @MichaëlAzevedo, Like I mention in the question I tried to do that, But when the imageview is resized in the UIView it's getting centred in the container. – Emil Adz Jan 21 '16 at 13:00
  • fix the UIImageView's aspect ratio: http://stackoverflow.com/questions/31334017/how-to-set-aspect-ratio-constraint-programmatically-in-ios, and then set the UIImageView's contentMode to aspect fit – Surely Jan 21 '16 at 14:20
  • You can use Objective-C classes in your swift project without any hassle: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html. So you should be able to use the custom UIImageViewAligned. Then in storyboard, just set the Custom Class of your UIImage to be UIImageViewAligned – Teevus Mar 29 '16 at 01:43

0 Answers0