Using ScrollView Programmatically in Swift 3

后端 未结 5 589
执念已碎
执念已碎 2020-11-28 11:00

I have searched other questions and seem to still have some trouble creating my scrollView programmatically with autolayout in swift 3. I am able to get my scrollview to sho

5条回答
  •  眼角桃花
    2020-11-28 11:47

    Set scrollview images to the wallpaper:

    @IBOutlet var scroll_view_img: UIScrollView!
    
    var itemPhotoList = NSMutableArray()
    
    var button = NSMutableArray()    
    
    @IBOutlet var imageview_big: UIImageView!
    
    override func viewDidLoad() {
    
        super.viewDidLoad()
        itemPhotoList = ["grief-and-loss copy.jpg","aaa.jpg","image_4.jpeg"]        
    
        // button = ["btn1","btn2"]
    
        let width:CGFloat = 100
        let height:CGFloat = 100
        var xposition:CGFloat = 10
        var scroll_contont:CGFloat = 0
    
        for i in 0 ..< itemPhotoList.count
        {
            var button_img = UIButton()
            button_img = UIButton(frame: CGRect(x: xposition, y: 50, width: width, height: height))
            let img = UIImage(named:itemPhotoList[i] as! String)
            button_img.setImage(img, for: .normal)
            scroll_view_img.addSubview(button_img)
            button_img.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
            button_img.tag = i
            view.addSubview(scroll_view_img)
            xposition += width+10
            scroll_contont += width
            scroll_view_img.contentSize = CGSize(width: scroll_contont, height: height)
        }
    }
    
    func buttonAction(sender: UIButton!)
    {
        switch sender.tag {
        case 0:
            imageview_big.image = UIImage(named: "grief-and-loss copy.jpg")
        case 1:
            imageview_big.image = UIImage(named: "aaa.jpg")
        case 2:
            imageview_big.image = UIImage(named: "image_4.jpeg")
        default:
            break
        }
    }
    

提交回复
热议问题