Scroll view in swift

Asked 2 years ago, Updated 2 years ago, 31 views

I am currently making a simple scroll view with swift (sliding to the next picture).I was able to display the image, but the screen is full and I can't display it beautifully with the aspect ratio adjusted.

scrollView.contentMode=UIViewContentMode.ScaleAspectFit
I'm inserting , but what's wrong with it?

Professor, please.

import UIKit

classViewController:UIViewController {
    varscrollView —UIScrollView!
    varpageImagesArr=["1.jpg", "2.jpg", "3.jpg" ];

    override func viewDidLoad(){
        super.viewDidLoad()
        let width=self.view.frame.maxX, height=self.view.frame.maxY
        let pageSize=self.pageImagesArr.count;

        // Create ScrollView
        scrollView = UIScrollView (frame:self.view.frame)
        scrollView.pagingEnabled=true
        scrollView.frame = CGRectMake(0,0, width, height);
        scrollView.contentSize=CGSizeMake (CGFloat(pageSize)*width, 0)
        __scrollView.contentMode=UIViewContentMode.ScaleAspectFit__

        // Create each page
        for vari=0;i<pageSize;i++{
            let img: UIImage=UIImage(named:self.pageImagesArr[i])!;
            letiv: UIImageView= UIImageView (image:img);
            iv.frame = CGRectMake (CGFloat(i)*width, 0, width, height);
            scrollView.addSubview(iv)
        }
        self.view.addSubview(scrollView)
    }
    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
    }
}

swift

2022-09-30 18:44

1 Answers

I'm sorry that I couldn't check it on hand, but
Why don't you set up AspectFit in the UIImageView?

//Create each page
for vari=0;i<pageSize;i++{
    let img: UIImage=UIImage(named:self.pageImagesArr[i])!;
    letiv: UIImageView= UIImageView (image:img);
    // hold the aspect ratio of an image
    iv.contentMode=UIViewContentMode.ScaleAspectFit
    iv.frame = CGRectMake (CGFloat(i)*width, 0, width, height);
    scrollView.addSubview(iv)
}


2022-09-30 18:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.