Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

UICollectionView not responding to its width iboutlet in swift 5 and iOS 14

Can someone please help with a question on constraint properties which are connected to iboutlets? I have a collection view which can span up to the width of the screen. It is a horizontal regular flow layout and if there are not enough cells the collection's container view is supposed to set to the width to the number of items it has and center itself in the superview. I defined a width constraint for the collection view set to the width of the superview and made the constraint an iboutlet in its viewcontroller. After the viewcontroller loads its data the viewcontroller sets the collection view width constraint to either the superview width if all the items are too large to fit or the width of the items. In my example case which has only one item it calculates the width of the item correctly and sets the iboutlet to that number. However, by looking at the view debugging display tool and examining the collection view when I do a debug print I see it set to the original width I set it to in the storyboard. I can see I have the correct iboutlet but I am not sure what I am doing incorrectly. Can someone please help? Thanks

enter image description here

The debug print out for the collection view after the iboutlet width property is set looks like this

<UICollectionView: 0x15c13b400; frame = (0 496; 414 100); clipsToBounds = YES; autoresize = RM+BM; tag = 2; gestureRecognizers = <NSArray: 0x283d53f00>; layer = <CALayer: 0x28337ff80>; contentOffset: {0, 0}; contentSize: {95, 100}; adjustedContentInset: {0, 0, 0, 0}; layout: <FelineFinder.HorizontalLayoutVaryingWidths: 0x1610063a0>; dataSource: <FelineFinder.AdoptableHeaderTableViewCell: 0x15c0d9200; baseClass = UITableViewCell; frame = (0 0; 414 584); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x28337dee0>>>

The 414 value should be 100 in my test case.

Addendum to problem: I am not getting the right visual results. The collection view stays the width of the screen even though I set the correct constraint value in code. I traced the code and put a breakpoint at the spot where this code is run and although the width constraint iboutlet is being set the collection view container does not get set the proper width both visually and when I do the debug as above. This is being run in ViewDidLoad of the correct ViewController. Here is the code I use:

var mediaWidth: CGFloat = 0.0
for m in media {
    if m.cellType == .image {
        let photo = m as! imageTool
        let ratio = CGFloat(100.0) / CGFloat(photo.thumbNail.height)
        mediaWidth += CGFloat(photo.thumbNail.width) * ratio
    } else if m.cellType == .video {
        mediaWidth += 133
    }
}
if CGFloat(mediaWidth) < self.contentView.frame.width {
    MediaToolBarWidth.constant = CGFloat(mediaWidth)
} else {
    MediaToolBarWidth.constant = self.contentView.frame.width
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...