gpt4 book ai didi

ios - 如何使导航 Controller 中的嵌入与scrollView一起使用

转载 作者:行者123 更新时间:2023-11-30 11:56:01 24 4
gpt4 key购买 nike

我看过这个教程 Snapchat Like Layout using View Controller Theme然后我在导航 Controller 中嵌入了三个 Controller 之一,但它不起作用然后我尝试将它添加到 View Controller 上 ScrollView 怎么也不起作用+给了我重叠我没有在代码中尝试过不过,我不认为这是原因,这是代码

i hope this may help

class ViewController: UIViewController {


@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()

let V1 = self.storyboard?.instantiateViewController(withIdentifier: "V1") as UIViewController!
self.addChildViewController(V1!)
self.scrollView.addSubview((V1?.view)!)
V1?.didMove(toParentViewController: self)
V1?.view.frame = scrollView.bounds

let V2 = self.storyboard?.instantiateViewController(withIdentifier: "V2") as UIViewController!
self.addChildViewController(V2!)
self.scrollView.addSubview((V2?.view)!)
V2?.didMove(toParentViewController: self)
V2?.view.frame = scrollView.bounds

var V2Frame: CGRect = V2!.view.frame
V2Frame.origin.x = self.view.frame.width
V2?.view.frame = V2Frame

let V3 = self.storyboard?.instantiateViewController(withIdentifier: "V3") as UIViewController!
self.addChildViewController(V3!)
self.scrollView.addSubview((V3?.view)!)
V3?.didMove(toParentViewController: self)
V3?.view.frame = scrollView.bounds

var V3Frame: CGRect = V3!.view.frame
V3Frame.origin.x = 2 * self.view.frame.width
V3?.view.frame = V3Frame

self.scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)
self.scrollView.contentOffset = CGPoint(x: self.view.frame.width * 1 , y: self.view.frame.height)

}

代码是如何工作的?1-首先我将 ScrollView 添加到 View Controller 2-然后在 ScrollView 中我添加了 3 个 View Controller ,并且所有 3 个 Controller 都有自己的类,如普通 View Controller
3- ScrollView 通过顶部的这段代码在其 View 上添加了 3 个 Controller 所有 3 个 Controller 都继承自自己的类,一切都很好如果有人可以提供帮助或需要更多描述,请询问

最佳答案

try below


Create ViewController in Storyboard or XIB and add ScrollView and PageControl

My Class as below:


class ScrollContentViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}


override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.pageControl.currentPage = 0
self.pageControl.numberOfPages = 4
var originX = CGFloat(0)
let width = scrollView.frame.size.width
let height = scrollView.bounds.size.height
for x in 0 ..< 4 {
let V1 = UIViewController()
self.scrollView.addSubview(V1.view)
V1.view.frame = CGRect(x: originX, y: 0, width: width, height: height)
V1.view.backgroundColor = x % 2 == 0 ? UIColor.lightGray : UIColor.darkGray
self.addChildViewController(V1)
originX += width
}
self.scrollView.contentSize = CGSize(width: originX, height: height)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let x = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
self.pageControl.currentPage = x
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

关于ios - 如何使导航 Controller 中的嵌入与scrollView一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47815033/

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