我的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
}
}
感谢。
答案 0 :(得分:0)
我找到了答案,这只是我的ViewController的这个属性:
self.automaticallyAdjustsScrollViewInsets
您必须将此属性设置为false(默认情况下为true)。 文档说明解释了它:
此属性的默认值为true,这使容器视图控制器知道他们应该调整此视图控制器视图的滚动视图插图,以考虑状态栏,搜索栏,导航栏,工具栏所占用的屏幕区域,或标签栏。如果视图控制器实现管理自己的滚动视图插入调整
,请将此属性设置为false