2017-04-11 49 views
0

我的UIScollView存在問題,無法正常工作。水平分頁不適用於TabBarController和NavigationBarController

所以這是我對這部影片的問題:https://www.youtube.com/watch?v=IeeCoGm9-b0&feature=youtu.be

這是代碼聯想到:

CODE 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代碼:

CODE 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

我找到了答案,這是我的ViewController的只是這個屬性:

self.automaticallyAdjustsScrollViewInsets 

您必須將此屬性設置爲false(默認爲true)。 文檔的描述解釋:

此屬性的默認值是true,它可以讓容器視圖控制器知道自己應該調整該視圖控制器的視圖的滾動視圖插圖佔所消耗的屏幕區域狀態欄,搜索欄,導航欄,工具欄或選項卡欄。如果您的視圖控制器實施管理其自己的滾動視圖插入調整,則將此屬性設置爲false

相關問題