gpt4 book ai didi

swift - 水平分页不适用于 TabBarController 和 NavigationBarController

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

我的 UIScollView 出现问题,无法正常工作。

所以这是我在这个视频中遇到的问题:https://www.youtube.com/watch?v=IeeCoGm9-b0&feature=youtu.be

这是关联到的代码:

代码 1:

let screenSize = UIScreen.main.bounds
let frameHeight: CGFloat = 100
let heightOfNewsCell : CGFloat = 100
var timerForNews = Timer()
var plugeeNews = ["Plugee permet de partager vos fiches de révision !","Ajoutez à vos favoris les fiches de vos amis !"
,"Pourquoi ne pas invitez vos amis a rejoindre Plugee ?","Vous ne savez pas comment utiliser Plugee ? Cliquez ici !"]

let newsOfPlugScrollView : UIScrollView = {
let sv = UIScrollView()
sv.translatesAutoresizingMaskIntoConstraints = false
sv.isPagingEnabled = true
sv.backgroundColor = .gray
return sv
}()

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(r: 227, g: 228, b: 231)
self.navigationItem.title = "News"
setupMyViews()
setupNewsScrollView()

}
func setupMyViews() {
self.view.addSubview(newsOfPlugScrollView)

newsOfPlugScrollView.topAnchor.constraint(equalTo: self.view.topAnchor,constant : 100).isActive = true
newsOfPlugScrollView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
newsOfPlugScrollView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
newsOfPlugScrollView.heightAnchor.constraint(equalToConstant: frameHeight).isActive = true
}

func setupNewsScrollView() {
newsOfPlugScrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: frameHeight)

let view1 = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: frameHeight))
view1.translatesAutoresizingMaskIntoConstraints = false

let view2 = UIView(frame: CGRect(x: view.frame.width, y: 0, width: view.frame.width, height: frameHeight))
view1.backgroundColor = .red
view2.backgroundColor = .blue
newsOfPlugScrollView.addSubview(view1)
newsOfPlugScrollView.addSubview(view2)
newsOfPlugScrollView.contentSize = CGSize(width: view.frame.width * 2, height: frameHeight)
}

我在一个空项目中测试了同一个类(代码 1),它工作得很好( https://www.youtube.com/edit?o=U&video_id=Z0YvNwtYvAc )。所以我认为问题来自 tabBarController 代码(但我不知道为什么)。

这是 tabBarController 代码:

代码 2:

 class TabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()

var arrayViews : [UIViewController] = [createAViewController(controller: NewsController(), image: #imageLiteral(resourceName: "IconeTabBar"))]

arrayViews.append(createAViewController(controller: HomeController(), image: #imageLiteral(resourceName: "IconeTabBar")))
arrayViews.append(createAViewController(controller: TopController(), image: #imageLiteral(resourceName: "IconeTabBar")))

viewControllers = arrayViews
self.selectedIndex = 1
}

private func createAViewController(controller : UIViewController, image : UIImage) -> UINavigationController {
let controller = controller
let navController = UINavigationController(rootViewController: controller)
navController.tabBarItem.title = ""
navController.tabBarItem.image = image
navController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
navController.tabBarItem.title = nil
return navController
}
}

谢谢。

最佳答案

我找到了答案,这就是我的 ViewController 的这个属性:

self.automaticallyAdjustsScrollViewInsets

您必须将此属性设置为 false(默认为 true)。文档的描述对此进行了解释:

The default value of this property is true, which lets container view controllers know that they should adjust the scroll view insets of this view controller’s view to account for screen areas consumed by a status bar, search bar, navigation bar, toolbar, or tab bar. Set this property to false if your view controller implementation manages its own scroll view inset adjustments

关于swift - 水平分页不适用于 TabBarController 和 NavigationBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356342/

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