เหตุใดข้อจำกัดจึงไม่ทำงาน

โหมดแนวตั้ง

ป้อนคำอธิบายรูปภาพที่นี่

โหมดแนวนอน

ป้อนคำอธิบายรูปภาพที่นี่

Tab Bar ใช้งานโดยใช้ UICollectionView (CV) ใน CV ฉันได้เพิ่ม 4 รายการ:

addSubview(collectionTab)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":collectionTab]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":collectionTab]))

เมื่อฉันเปลี่ยนการวางแนว ข้อจำกัดจะไม่ทำงาน ทำไม ฉันจะแก้ไขได้อย่างไร? ฉันคิดว่าขนาดของรายการจะไม่ถูกคำนวณใหม่เมื่อฉันเปลี่ยนมุมมอง

ขนาดของรายการ:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: frame.width/4, height: frame.height)
}

person maxim rad    schedule 17.11.2016    source แหล่งที่มา
comment
ขนาดของรายการ func collectionView(_ collectionView: UICollectionView, เค้าโครง collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { คืน CGSize (ความกว้าง: frame.width/4, ความสูง: frame.height) }   -  person maxim rad    schedule 17.11.2016
comment
collectionTab คืออะไร? นั่นคือชื่อของ UICollectionView หรือไม่   -  person Sajjon    schedule 17.11.2016
comment
ใช่ นี่คือมุมมองคอลเลกชัน   -  person maxim rad    schedule 17.11.2016


คำตอบ (1)


ลองรัน invalidateLayout บน UICollectionViewLayout บน UICollectionView ของคุณ ถูกใจสิ่งนี้

override func viewWillLayoutSubviews {
    myCollectionView.collectionViewLayout.invalidateLayout()
}
person Sajjon    schedule 17.11.2016
comment
เมื่อรันฟังก์ชั่นนี้? - person maxim rad; 17.11.2016
comment
เพียงใช้สิ่งนี้ใน UIViewController ของคุณ มันจะถูกเรียกโดยอัตโนมัติ - person Sajjon; 17.11.2016
comment
ฉันคิดว่าขนาดของรายการจะไม่ถูกคำนวณใหม่เมื่อฉันเปลี่ยนมุมมอง - person maxim rad; 17.11.2016
comment
คุณกำลังใช้งาน collectionView(_:layout:sizeForItemAt:) หรือไม่? คุณควรคำนวณขนาดรายการที่นั่น! - person Sajjon; 17.11.2016
comment
func collectionView (_ collectionView: UICollectionView, โครงร่าง collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize (ความกว้าง: frame.width/4, ความสูง: frame.height) } - person maxim rad; 17.11.2016
comment
คุณช่วยตรวจสอบได้ไหมว่า CollectionView ได้รับเฟรมใหม่หลังจากการหมุน? โปรดพิมพ์ความกว้างและความสูงของ CollectionView ก่อนและหลังการหมุนภายในเมธอด viewWillLayoutSubviews - person Sajjon; 17.11.2016
comment
@maximrad คุณพิมพ์เฟรมก่อนและหลังการหมุนผลลัพธ์จะเป็นอย่างไร? - person Sajjon; 17.11.2016
comment
ขอบคุณมากครับ!! - person maxim rad; 17.11.2016
comment
แทนที่ func viewWillLayoutSubviews () { tabBar.collectionTab.collectionViewLayout.invalidateLayout() } - person maxim rad; 17.11.2016