2016-10-10 22 views
0

iCarouselビューにXIB UIを使用してカスタムビューを追加しようとしていますが、初期化して実行すると、カルーセルビューのサブビューの幅が自動的に変更されます。iCarouselカスタムビューxibはサブビュー幅を自動的に変更します

func carousel(carousel: iCarousel!, viewForItemAtIndex index: Int, reusingView view: UIView!) -> UIView! { 

    var itemView: ProfileQuestionView 

    //create new view if no view is available for recycling 
    if (view == nil) { 

     itemView = UINib.init(nibName: "ProfileQuestionView", bundle: nil).instantiateWithOwner(self, options: nil)[0] as! ProfileQuestionView 
     itemView.frame    = CGRect.init(x: 20, y: 10, width: self.view.frame.width-40, height: self.carouselView.frame.height-20) 

     itemView.layer.borderColor = UIColor.clearColor().CGColor 
     itemView.layer.borderWidth = 0.5 
     itemView.layer.cornerRadius = 5.0; 
     itemView.layoutIfNeeded() 

    } else { 

     //get a reference to the label in the recycled view 
     itemView = view as! ProfileQuestionView 

     itemView.layoutIfNeeded() 
    } 

    return itemView 
} 

この問題が表示されている理由を知ってはいけません。

carouselView.type = iCarouselType.Rotary 
+0

あなたにタグを付けてアイテムビュー 'itemView.tag = 1'はfrの作成後に表示されますom nibと 'else'はタグ' itemView =(view?.viewWithTag(1))! 'でブロックをブロックします。それが問題を解決するかどうかを確認してください。 'layoutIfNeeded'を呼び出す必要はありません –

答えて

0

ちょうどヒットして試してみてください。それが動作するかどうかは分かりません。

は、あなたはまだ、まだ設定されていない viewDidLoad制約であなたのカルーセルをリロードしているかのように

itemView.frame = CGRect.init(x: 20, y: 10, width: UIScreen.mainScreen().bounds.width-40, height: self.carouselView.frame.height-20) 

にあなたのこのライン

itemView.frame = CGRect.init(x: 20, y: 10, width: self.view.frame.width-40, height: self.carouselView.frame.height-20) 

を変更してみてください。

また、タグでアイテムを取得してみてください。

if (view == nil) { 
    itemView = UINib.init(nibName: "ProfileQuestionView", bundle: nil).instantiateWithOwner(self, options: nil)[0] as! ProfileQuestionView 

    // give a tag 
    itemView.tag = 1 
    //Then your rest of the code 
    } else { 
    // Reusability 
    itemView = (view?.viewWithTag(1))! 
} 

この問題を修正してください。

0

私はXcode 8でこの問題に直面しましたが、私のコードはXcode 7.3.1でうまく動作します。 あなたの初期化カルーセル)がviewDidLayoutSubviews(中カルーセルの種類を初期化しviewDidLayoutSubviews

override func viewDidLayoutSubviews() { 
     super.viewDidLayoutSubviews() 
     //Check if carousel is initialized or not 
     if self.carousel.dataSource == nil { 
      //Init your carousel here 
     } 
    } 
0

に入れ

方法viewDidLayoutSubviews()

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    // carousel type 
    yourCarouselName.type = .custom 
} 
関連する問題