2017-02-14 38 views
0

我喜歡在我的swift項目中使用iCarousel,但有一件事我無法克服;我想在我的項目中使用Visual語言來佈局視圖,但是每當我使用iCarousel的可視化格式時,它都不起作用。在Swift中使用視覺格式語言和iCarousel

我注意到問題是TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false屬性。

每當我禁用此屬性,我的視覺格式約束被禁用的iCarousel,並且每當我啓用它,約束完美,但我的iCarousel將不會滾動並保持始終。

當前代碼:

import UIKit 
import iCarousel 

class Step2_HomePage: UIViewController,iCarouselDelegate,iCarouselDataSource  { 


let TopMenuCarouselCount = 5 

    var TopMenuCarousel = iCarousel() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    print("Step2HomePage icinde") 



    TopMenuCarousel = iCarousel(frame: CGRect()) 
    view.addSubview(TopMenuCarousel) 
    // TopMenuCarousel.clipsToBounds = true 
    TopMenuCarousel.type = .Linear 
    TopMenuCarousel.dataSource = self 
    TopMenuCarousel.delegate = self 






let views = [ "TopMenuCarousel": TopMenuCarousel ] 

    // 2 
    var allConstraints = [NSLayoutConstraint]() 



    let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
     "V:|-100-[TopMenuCarousel]", 
     options: [], 
     metrics: nil, 
     views: views) 
    allConstraints += TopMenuCarouselTop 

    let TopMenuCarouselHorizontal = NSLayoutConstraint.constraintsWithVisualFormat(
     "H:|-0-[TopMenuCarousel]-0-|", 
     options: [], 
     metrics: nil, 
     views: views) 
    allConstraints += TopMenuCarouselHorizontal 



    TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false 
    NSLayoutConstraint.activateConstraints(allConstraints) 




    // Do any additional setup after loading the view, typically from a nib. 
} 

func numberOfItemsInCarousel(carousel: iCarousel) -> Int { 
    print("carousel number") 
    return TopMenuCarouselCount 

} 

func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView { 
    print("carousel view icinde") 
    let tempView = UIView(frame: CGRect(x: 0, y: 0 , width: 20, height: 20)) 

    tempView.backgroundColor = UIColor.blueColor() 
    return tempView 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


} 

回答

0

你好像沒有什麼高度設置爲您iCarousel對象。嘗試將您的第一個約束更改爲:

let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:|-100-[TopMenuCarousel(20)]", 
    options: [], 
    metrics: nil, 
    views: views) 

這是您的原始代碼的完整修改版本。我提出了更大的意見(你的是20x20),並添加了一些顏色,以便更容易看到發生了什麼。

import UIKit 
import iCarousel 

class Step2_HomePage: UIViewController,iCarouselDelegate,iCarouselDataSource { 

    // array of colors to make it easy to see the individual Carousel views 
    let arrayOfColors = [ UIColor.blueColor(), UIColor.redColor(), UIColor.yellowColor(), UIColor.orangeColor(), UIColor.greenColor()] 

    let TopMenuCarouselCount = 5 

    var TopMenuCarousel = iCarousel() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     print("Step2HomePage icinde") 

     self.view.backgroundColor = UIColor.lightGrayColor() 

     // initialize the TopMenuCarousel object 
     TopMenuCarousel = iCarousel(frame: CGRect()) 

     // add TopMenuCarousel to the view 
     view.addSubview(TopMenuCarousel) 

     // if clipsToBounds == true, TopMenuCarousel subviews will be clipped to the TopMenuCarousel frame 
     // default is false 

     //  TopMenuCarousel.clipsToBounds = true 

     TopMenuCarousel.type = .Linear 
     TopMenuCarousel.dataSource = self 
     TopMenuCarousel.delegate = self 

     TopMenuCarousel.backgroundColor = UIColor.purpleColor() 

     let views = [ "TopMenuCarousel": TopMenuCarousel ] 

     var allConstraints = [NSLayoutConstraint]() 

     // position TopMenuCarousel 100 from the Top, with a Height of 200 
     let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
      "V:|-100-[TopMenuCarousel(200)]", 
      options: [], 
      metrics: nil, 
      views: views) 

     allConstraints += TopMenuCarouselTop 

     // set TopMenuCarousel to stretch the full Width of the view 
     let TopMenuCarouselHorizontal = NSLayoutConstraint.constraintsWithVisualFormat(
      "H:|-0-[TopMenuCarousel]-0-|", 
      options: [], 
      metrics: nil, 
      views: views) 

     allConstraints += TopMenuCarouselHorizontal 

     // this property *must* be set to false 
     TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false 

     NSLayoutConstraint.activateConstraints(allConstraints) 

    } 

    func numberOfItemsInCarousel(carousel: iCarousel) -> Int { 
     print("carousel number \(TopMenuCarouselCount)") 
     return TopMenuCarouselCount 
    } 

    func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView { 
     print("carousel view icinde", index) 

     // create a 200 x 160 view to add to TopMenuCarousel 
     let tempView = UIView(frame: CGRect(x: 0, y: 0 , width: 200, height: 160)) 

     // give it one of the colors 
     tempView.backgroundColor = arrayOfColors[index % arrayOfColors.count] 

     return tempView 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 
+0

大家好, Unfortuntely它沒有工作:( 我確實如你所說,但這個時候,iCarousel觀點並沒有在constaint聲明的垂直高度。 的觀點出現在最左。最多 與我們的任何「垂直」調整屏幕的一角有什麼可以的原因當我刪除的方式,下面一行: 'TopMenuCarousel.translatesAutoresizingMaskIntoConstraints = FALSE' –

+0

我編輯我的回答給你一個完整的示例...''TopMenuCarousel.translatesAutoresizingMaskIntoConstraints' *必須*設置爲false,以便增加約束條件。 – DonMag

0

約束被創建和激活,但從未添加到視圖,您應該使用:

view.addConstraints(allConstraints) 

NSLayoutConstraint.activateConstraints(allConstraints) 

另外,作爲DonMag說,沒有高度的限制,這意味着如果iCarousel視圖沒有正確的固有尺寸,它將不會顯示,您應該添加一個像DonMag寫的示例一樣的明確高度。

+0

曾經是這種情況......從Apple的文檔:'爲iOS 8.0或更高版本開發時,請使用NSLayoutConstraint類的activateConstraints:方法,而不是直接調用addConstraints:方法。 activateConstraints:方法會自動將約束添加到正確的視圖。「https://developer.apple.com/reference/uikit/uiview/1622513-addconstraints – DonMag

+0

哇,我不知道。所以唯一的問題應該是你說的高度! – omarzl

+0

大家好, Unfortuntely它沒有工作:( 我確實如你所說,但這個時候,iCarousel觀點並沒有在constaint聲明的垂直高度。 的觀點出現在最左上方的與我們的任何「垂直」調整屏幕 可能是什麼原因?順便說一下,我刪除,以下行: 'TopMenuCarousel。translatesAutoresizingMaskIntoConstraints = false' –

相關問題