IOS - 创建自定义窗口小部件时的布局问题

时间:2016-01-04 09:36:16

标签: ios swift layout widget xib

我使用xib文件为UICollectionView包装器和可重复使用的UICollectionViewCell创建了一个Carousel小部件。

窗口小部件由两个UICollectionView布局组成,一个在另一个上面。顶部UICollectionView用于捕获用户事件,并控制两个UICollectionView的内容偏移量容器。我采用这种方法,以分页大小不同于UICollectionView宽度滚动(按项目大小滚动而不是页面)。

当我用框架初始化时,一切正常,但如果我正在使用故事板,分配UIView我的轮播小部件类,布局就搞乱了

使用框架以编程方式添加顶视图: screen shot, layout issues

如果我调试视图层次结构,则表明没有问题: enter image description here

enter image description here

滚动底部轮播后,视图布局正确。

这是我的小部件和单元格的代码:

import UIKit

class CarouselWidgetView: UIView ,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{

@IBOutlet weak var dummyCollectionView: UICollectionView!
@IBOutlet weak var mainCollectionView: UICollectionView!
@IBOutlet var mainView: UIView!
@IBOutlet weak var dummyWidth: NSLayoutConstraint!
@IBOutlet weak var topLabel: UILabel!

enum Events : String {
    case ON_ITEM_SELECTED  = "onItemSelected"
}



var items: NSArray? = nil {
    didSet {
        //reload data
        dummyCollectionView.reloadData()
        mainCollectionView.reloadData()
    }
}

var frameRatio:CGFloat = 0.9
var didLayoutSubviews = false
var didSetDummyCollection = false

@IBInspectable var framePercent: CGFloat {
    get {
        return frameRatio
    }
    set {
        frameRatio = newValue
    }
}


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(frame: CGRect) {
    super.init(frame: frame)
    NSBundle.mainBundle().loadNibNamed("carouselWidget", owner: self, options: nil)
    mainCollectionView.registerNib(UINib(nibName: "carouselCell", bundle: nil), forCellWithReuseIdentifier: "carouselCell")
    dummyCollectionView.registerNib(UINib(nibName: "carouselCell", bundle: nil), forCellWithReuseIdentifier: "carouselCell")
    mainView.frame = CGRectMake(0, 0, self.frame.width, self.frame.height)
    self.addSubview(mainView)
    didLayoutSubviews = true
}

override func layoutSubviews() {
    if(!didLayoutSubviews){
        NSBundle.mainBundle().loadNibNamed("carouselWidget", owner: self, options: nil)
        mainCollectionView.registerNib(UINib(nibName: "carouselCell", bundle: nil), forCellWithReuseIdentifier: "carouselCell")
        dummyCollectionView.registerNib(UINib(nibName: "carouselCell", bundle: nil), forCellWithReuseIdentifier: "carouselCell")
        mainView.frame = CGRectMake(0, 0, self.frame.width, self.frame.height)
        self.addSubview(mainView)
        didLayoutSubviews = true
    }
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    if(!didSetDummyCollection){
        dummyWidth.constant = mainCollectionView.frame.width * frameRatio + 8
        didSetDummyCollection = true;
    }
    return CGSizeMake(mainCollectionView.frame.width * frameRatio + 4, mainCollectionView.frame.height)
}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if let length = items?.count{
        return length
    }
    return 0
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("carouselCell", forIndexPath: indexPath) as! CarouselCell
    cell.cellData = (items![indexPath.item] as! NSDictionary)
    cell.frame.origin.y = 0
    self.layoutIfNeeded()
    return cell
}

func scrollViewDidScroll(scrollView: UIScrollView) { 
    mainCollectionView.contentOffset = scrollView.contentOffset
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    NSNotificationCenter.defaultCenter().postNotificationName(CarouselWidgetView.Events.ON_ITEM_SELECTED.rawValue, object: ["data":(items![indexPath.item] as! NSDictionary) , "cell":collectionView.cellForItemAtIndexPath(indexPath)!])
}


}

单元格代码:

import UIKit

class CarouselCell: UICollectionViewCell {
@IBOutlet weak var backgroundImage: UIImageView!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var tags: UILabel!

var cellData:NSDictionary? = nil{
    didSet {
        if let imgUrl = cellData!["image"] as? String{
            backgroundImage.loadImageFromURL(imgUrl)
        }

        if let poiName = cellData!["name"] as? String{
            name.text = poiName
        }

        if let tagsDesc = cellData!["tags"] as? String{
            tags.text = tagsDesc
        }
    }
}

override init(frame: CGRect) {
    super.init(frame: CGRectMake(frame.origin.x, 0, frame.width, frame.height))

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}
}

我错过了什么?

0 个答案:

没有答案