Scale image to fit screen on iPhone rotation

前端 未结 2 2069
别那么骄傲
别那么骄傲 2021-01-02 12:45

I have a UIScrollView containing an UIImageView and I have some trouble to get the correct behavior when the iPhone rotates.

Goal: I am trying to o

相关标签:
2条回答
  • 2021-01-02 12:51

    I had a similar situation, a very simple scrollView with 3 imageViews ('previous', 'current', and 'next') to display a series of photos.

    The photos were all sized for the iPad, but a mix of landscape and portrait orientations. I wanted them to fit into the screen, (i.e. letterboxed if necessary), with aspect ratio retained. I found I only needed to do this to the 3 imageViews for it to work:

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    0 讨论(0)
  • 2021-01-02 13:13

    Try this:

    scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                               UIViewAutoresizingFlexibleHeight);    
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                               UIViewAutoresizingFlexibleHeight);
    
    imageView.contentMode = UIViewContentModeAspectFit; // You could also try UIViewContentModeCenter
    

    I'm not sure if the imageView will get automatically resized within a UIScrollView, so if the autoresizing doesn't work, you could try setting the frame yourself on rotate to equal the size of the scrollview.

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