2015-02-24 59 views
1

我希望我的scrollview的框架與superview的框架大小相同。並設置contentize,使其包含所有的imageViews。帶Autolayout的scrollview尺寸:緊湊型/任意尺寸類型的框架是600x480,大於iphone5?

問題是與AutoLayout,寬度和高度是600 x 480,我使用iphone5和尺寸類是w:Compact/h:Any。(我預計類似於:320 x 523)。
我的約束是:平等與寬度上海華,高度相等與上海華盈(乘數0.8),水平中心X和垂直中心Y.

let pagesScrollViewSize = scrollView.frame.size 
println("pagesScrollViewSize : \(pagesScrollViewSize)") //600x480 
scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageCount), pagesScrollViewSize.height) 

會有人知道如何解決這個問題? imageview看起來比屏幕的邊框大,所以.SizeAspectFit不符合正確的尺寸。而不是滾動到另一個imageView,它滾動查看圖像的其餘部分,我不想要。

編輯:
這是ViewController.swift整個代碼:

class ViewController: UIViewController, UIScrollViewDelegate { 

    @IBOutlet weak var pageControl: UIPageControl! 
    @IBOutlet weak var scrollView: UIScrollView! 

    var pageViews: [UIImageView?] = [] 
    var pageImages : [UIImage?] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     pageImages = [UIImage(named:"photo1.png"), 
     UIImage(named:"photo2.png"), 
     UIImage(named:"photo3.png"), 
     UIImage(named:"photo4.png")] 

     let pageCount = pageImages.count 
     pageControl.currentPage = 0 
     pageControl.numberOfPages = pageCount 

     for _ in 0..<pageCount { 
      pageViews.append(nil) 
     } 

     let pagesScrollViewSize = scrollView.frame.size 
     println("pagesScrollViewSize : \(pagesScrollViewSize)") 
     scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageCount), pagesScrollViewSize.height) 
     loadVisiblePages() 
    } 

    //when scrolling 
    func scrollViewDidScroll(scrollView: UIScrollView!){ 
     loadVisiblePages() 
    } 

    func loadVisiblePages(){ 
     let pageWidth = scrollView.frame.size.width 
     let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth)/(pageWidth*2.0))) 
     println("page: \(page)") 
     pageControl.currentPage = page 
     let firstPage = page-1 
     let lastPage = page+1 
     //remove all the pages before firstPage 
     for var index = 0; index < firstPage; ++index{ 
      purgePage(index) 
     } 
     //load pages in our range 
     for var index = firstPage; index <= lastPage; ++index { 
      loadPage(index) 
     } 
     //remove after lastPage 
     for var index = lastPage+1; index < pageImages.count; ++index { 
      purgePage(index) 
     } 
    } 

    func loadPage(index:Int){ 
     if index < 0 || index >= pageImages.count { 
      return 
     } 
     if let pageView = pageViews[index] { 
      //already loaded 
     } 
     else { 
      var frame = scrollView.bounds 
      frame.origin.x = frame.size.width * CGFloat(index) 
      frame.origin.y = 0.0 
      println("\(frame)") 

      var newImageView = UIImageView(image:pageImages[index]) 
      newImageView.contentMode = .ScaleAspectFit 
      newImageView.frame = frame 
      scrollView.addSubview(newImageView) 

      pageViews[index] = newImageView 
     } 
    } 

    func purgePage(index:Int){ 
     if index < 0 || index >= pageImages.count { 
      return 
     } 
     if let pageView = pageViews[index] { 
      pageView.removeFromSuperview() 
      pageViews[index] = nil 
     } 
    } 

} 

感謝

+0

你在哪裏檢查這些幀? – bjtitus 2015-02-24 18:39:29

+0

@bjtitus我編輯我的消息與代碼 – Paul 2015-02-24 19:29:56

回答

4

尺寸類(或任何自動佈局的變化,對於這個問題)viewDidAppear或之後不會應用到viewDidLayoutSubviews。您需要將這些代碼移入其中。當它從Interface Builder文檔(xib或storyboard)中加載出來時,您會看到視圖的框架。在這些生命週期事件之後,你永遠不應該檢查幀。

Unable to set frame correctly before viewDidAppear

+0

非常好,謝謝。 – Paul 2015-02-24 20:42:30