我正在一個項目中,我必須以模態方式顯示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)
}
}
}
您是否設置了scrollView.contentSize? Autolayout設置(啓用時)? –
最後一行設置'.contentSize'。 OP沒有在'viewInsideScrollView'上顯示任何自動佈局約束,並且他/她的代碼顯然沒有分配任何約束。 – DonMag
我把所有這些代碼都轉移到了viewDidLayoutSubviews()中,它仍然不起作用,我認爲甚至應該使用自動佈局? –