for my UIImageView I choose Aspect Fit (InterfaceBuilder) but how can I change the vertical alignment?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- how do you prevent page scroll in textarea on mobi
- Custom UITableview cell accessibility not working
相关文章
- Could I create “Call” button in HTML 5 IPhone appl
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
- Open iOS 11 Files app via URL Scheme or some other
- Can keyboard of type UIKeyboardTypeNamePhonePad be
- Can not export audiofiles via “open in:” from Voic
If using Storyboards this can be achieved with constraints...
Firstly a UIView with the desired final frame / constraints. Add a UIImageView to the UIView. Set the contentMode to Aspect Fill. Make the UIImageView frame be the same ratio as the image (this avoids any Storyboard warnings later). Pin the sides to the UIView using standard constraints. Pin the top OR bottom (depending where you want it aligned) to the UIView using standard constraints. Finally add an aspect ratio constraint to the UIImageView (making sure ratio as the image).
Try setting:
This worked for me.
To align a scale-to-fit image use auto layout. Example right aligned image after scale-to-fit in UIImageView:
Your aspect fit scaled image is now right aligned in the UIImageView
Change the constraints to align differently within the imageview.
I used UIImageViewAligned for changing the alignment of image thanks to the developer
UIImageViewAligned
This is a bit tricky one since there is no option to set further alignment rules if you already selected a content mode (
.scaleAspectFit
).But here's a workaround to this:
First need to resize your source image explicitly by calculating dimensions (if it'd be in a
UIImageView
withcontentMode = .scaleAspectFit
).Then you simply need to call this function on your original image by passing your imageView's frame, and assign the result to your
UIImageView.image
property. Also make sure you set your imageView's desiredcontentMode
here (or even in the Interface Builder)!I know this is an old thread, but I thought I'd share what I did to easily change the clipped region from the top to the bottom of the image view in Interface Builder, in case anyone had the same problem I did. I had a UIImageView that filled the View of my ViewController, and was trying to make the top stay the same, independent of the size of the device's screen.
I applied the retina 4 form factor (Editor->Apply Retina 4 Form Factor).
I pinned the height and width.
Now, when the screen changes size, the UIImageView is actually the same size, and the view controller just clips what is off the screen. The frame origin stays at 0,0, so the bottom and right of the image are clipped, not the top.
Hope this helps.