2017-08-31 128 views
0

我正在一個項目中,我必須以模態方式顯示ViewController,但是,滾動視圖似乎不能正常工作。它不滾動。我沒有直接添加到scrollview,而是在scrollview中添加了一個視圖,然後我將數據添加到該視圖中。我檢查了scrollview的內容大小比scrollview的實際寬度大得多,但是它不會滾動。我禁用了自動佈局,沒有任何變化。這是我的代碼。UIScrollView不滾動模態,Swift

import Foundation 
import UIKit 

class VenueViewController: UIViewController, UIScrollViewDelegate{ 

    static var marker : Marker = Marker(v: "Error", lat: 1, lon: 1, az: 1, dist: 1000) 

    @IBOutlet weak var viewInsideScrollView: UIView! 
    @IBOutlet weak var dropDownView: UIView! 
    @IBOutlet weak var upcomingEventLabel: UILabel! 
    @IBOutlet var backgroundView: UIView! 
    @IBOutlet weak var venueNameLabel: UILabel! 
    @IBOutlet weak var currentEventLabel: UILabel! 
    @IBOutlet weak var upcomingEventScrollView: UIScrollView! 
    @IBOutlet weak var currentEventImageView: UIImageView! 


    override func viewWillAppear(_ animated: Bool){ 
     super.viewWillAppear(animated) 
     upcomingEventScrollView.isScrollEnabled = true 
     upcomingEventScrollView.showsVerticalScrollIndicator = true 
     upcomingEventScrollView.isUserInteractionEnabled = true 
     upcomingEventScrollView.isPagingEnabled = true 
     dropDownView.isUserInteractionEnabled = true 

     if VenueViewController.marker.getVenueName() == "Error" { 
      return 
     } 


     if !UIAccessibilityIsReduceTransparencyEnabled() { 
      self.backgroundView.backgroundColor = UIColor.clear 
      self.backgroundView.layer.zPosition = 0 
      self.dropDownView.layer.zPosition = 1 
      let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark) 
      let blurEffectView = UIVisualEffectView(effect: blurEffect) 
      blurEffectView.frame = self.backgroundView.bounds 
      blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
      self.backgroundView.addSubview(blurEffectView) 
     } else { 
      self.backgroundView.backgroundColor = UIColor.black 
     } 


     venueNameLabel.text = VenueViewController.marker.getVenueName() 
     currentEventLabel.text = VenueViewController.marker.getEvents()[0].getName() 
     ArtemisHelper.titleImageLoadView(imageView: currentEventImageView, event: VenueViewController.marker.getEvents()[0]) 

     loadUpcomingEvents() 
    } 

    func loadUpcomingEvents(){ 
     for event in VenueViewController.marker.getEvents(){ 
      if event.getName() == currentEventLabel.text{ 
       //continue 
      } 
      let eventTitleLabel : UILabel = UILabel() 
      eventTitleLabel.text=event.getName() + " " + event.getLocalDate() 
      eventTitleLabel.frame.size.width = dropDownView.frame.width 
      eventTitleLabel.frame.size.height = 20 

      eventTitleLabel.frame = CGRect(x: self.viewInsideScrollView.frame.minX, y: self.viewInsideScrollView.bounds.minY, width: self.viewInsideScrollView.frame.width, height: eventTitleLabel.frame.size.height) 

      eventTitleLabel.frame = eventTitleLabel.frame.offsetBy(dx: 0, dy: self.upcomingEventScrollView.contentSize.height) 
      self.viewInsideScrollView.addSubview(eventTitleLabel) 

      self.upcomingEventScrollView.contentSize = CGSize(width: self.upcomingEventScrollView.contentSize.width, height: self.upcomingEventScrollView.contentSize.height+eventTitleLabel.frame.height) 
      self.viewInsideScrollView.frame = CGRect(x: self.upcomingEventScrollView.bounds.minX, y: self.upcomingEventScrollView.bounds.minY, width: self.upcomingEventScrollView.frame.width, height: self.upcomingEventScrollView.contentSize.height) 

     } 
     print(self.viewInsideScrollView.frame.height) 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let touch : UITouch = touches.first! 
     let touchLocation = touch.location(in: self.view) 
     let obstacleViewFrame = self.view.convert(dropDownView.frame, from: dropDownView.superview) 

     if !obstacleViewFrame.contains(touchLocation) { 
      self.dismiss(animated: true, completion: nil) 
     } 
    } 
} 

From the Main Storyboard

回答

0

拆開代碼一塊一塊後,我發現DonMag是對在這個意義上,有是擋住了滾動型的透視圖。

self.backgroundView.addSubview(blurEffectView) 

在這裏,當我添加子視圖,這最終覆蓋,即使我設置z位置的東西比低滾動視圖滾動視圖。我添加了一個與backgroundView相鄰的視圖,並將模糊效果視圖添加到該視圖,這使我仍然可以訪問滾動視圖。

0

有許多優點,使用約束和自動佈局,但你肯定可以做你想做什麼沒有它。

您的「添加標籤和設置幀」循環非常複雜。看看這個方法:

func loadUpcomingEvents(){ 

    // this is our starting Label frame 
    // at 0,0 and dropDownViewWidth x 20 height 
    var labelRect = CGRect(x: 0, y: 0, width: dropDownView.frame.width, height: 20) 

    for event in VenueViewController.marker.getEvents(){ 

     // instantiate a new label, using the current labelRect for the frame 
     let eventTitleLabel : UILabel = UILabel(frame: labelRect) 

     // set the text of the label 
     eventTitleLabel.text=event.getName() + " " + event.getLocalDate() 

     // add the label to the "inside" view 
     self.viewInsideScrollView.addSubview(eventTitleLabel) 

     // increment the Y position of labelRect by the Height of labelRect 
     labelRect.origin.y += labelRect.size.height 

    } 

    // labelRect is now the frame of the "next label" in the list, 
    // if there was a next label, 
    // so we can use its Y position for the Height of the "inside" view 
    viewInsideScrollView.frame = CGRect(x: 0, y: 0, width: dropDownView.frame.width, height: labelRect.origin.y) 

    // and, we set the scroll view's contentSize to the size of the "inside" view 
    upcomingEventScrollView.contentSize = viewInsideScrollView.frame.size 
} 
+0

您是否設置了scrollView.contentSize? Autolayout設置(啓用時)? –

+0

最後一行設置'.contentSize'。 OP沒有在'viewInsideScrollView'上顯示任何自動佈局約束,並且他/她的代碼顯然沒有分配任何約束。 – DonMag

+0

我把所有這些代碼都轉移到了viewDidLayoutSubviews()中,它仍然不起作用,我認爲甚至應該使用自動佈局? –