gpt4 book ai didi

ios - 将 imageview 添加到 ScrollView 页面控件

转载 作者:行者123 更新时间:2023-11-30 11:19:37 27 4
gpt4 key购买 nike

下面的代码是使用页面控件的基本 ScrollView ,它在每个屏幕上使用基本颜色。我想在显示基本颜色的 uiview 上显示 imageview。我想从 Xcode 中的资源文件中调用 var pics。

var colors:[UIColor] = [UIColor.red, UIColor.blue, UIColor.green, UIColor.yellow]
var pics = ["a","b","c","d"]

override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(scrollView)
for index in 0..<4 {
frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.size = self.scrollView.frame.size

let subView = UIView(frame: frame)
subView.backgroundColor = colors[index]
self.scrollView .addSubview(subView)
}

func configurePageControl() {
self.pageControl.numberOfPages = colors.count
}

最佳答案

这里我假设您的项目中附加了 4 张名为 0,1,2,3 png 的图像,试试这个

import UIKit

class ViewController: UIViewController , UIScrollViewDelegate {

let scrollView = UIScrollView()

let pageCon = UIPageControl()

override func viewDidLoad() {

super.viewDidLoad()

let viewsCount = 4

var prevView = self.view!

scrollView.delegate = self

scrollView.isPagingEnabled = true

pageCon.numberOfPages = viewsCount

pageCon.currentPage = 0

pageCon.tintColor = .green

pageCon.currentPageIndicatorTintColor = .orange

pageCon.backgroundColor = .blue

pageCon.translatesAutoresizingMaskIntoConstraints = false

scrollView.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(scrollView)

view.insertSubview(pageCon, aboveSubview: scrollView)

NSLayoutConstraint.activate([

scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant:20),
scrollView.heightAnchor.constraint(equalToConstant: 400),
pageCon.centerXAnchor.constraint(equalTo: view.centerXAnchor),
pageCon.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant:-20),

])

for i in 0..<viewsCount {

let imageV = UIImageView()
imageV.image = UIImage(named: "\(i).png")
imageV.translatesAutoresizingMaskIntoConstraints = false

scrollView.addSubview(imageV)

if prevView == self.view {

imageV.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
}
else {

imageV.leadingAnchor.constraint(equalTo: prevView.trailingAnchor).isActive = true
}

NSLayoutConstraint.activate([

imageV.topAnchor.constraint(equalTo: scrollView.topAnchor),
imageV.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
imageV.widthAnchor.constraint(equalToConstant: self.view.frame.width),
imageV.heightAnchor.constraint(equalToConstant: 400)
])

if i == viewsCount - 1 {

imageV.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
}

prevView = imageV


}

}


func scrollViewDidScroll(_ scrollView: UIScrollView) {

pageCon.currentPage = Int(scrollView.contentOffset.x / self.view.frame.width)

}

}

关于ios - 将 imageview 添加到 ScrollView 页面控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51430146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com