使用CocoaPods遇问题:设置popupHeight提示只读属性无法赋值
Hey there! Let's break down what's happening here and how to fix it.
The error you're seeing means that the popupHeight property on BottomPopupViewController is designed to be read-only—you can't directly assign a value to it like you tried with popupVC.popupHeight = 50.0. Most CocoaPods popup components use this pattern to let you customize the height by overriding the property instead of setting it directly.
Here's the most common solution:
Subclass BottomPopupViewController and override the popupHeight getter to return your desired height:
import UIKit // Assuming BottomPopupViewController is imported from the pod class CustomBottomPopupVC: BottomPopupViewController { // Override the read-only property to return your custom height override var popupHeight: CGFloat { return 50.0 } }
Then update your original code to instantiate this subclass instead:
@IBAction func testBtnPressed(_ sender: Any) { guard let popupVC = storyboard?.instantiateViewController(withIdentifier: "popup") as? CustomBottomPopupVC else { return } popupVC.popupDelegate = self present(popupVC, animated: true, completion: nil) }
Other things to check:
- Some similar components might provide a writable alternative property (like
preferredPopupHeightorcustomPopupHeight). If you haven't already, double-check the component's internal docs or header files to see if such a property exists—if it does, you can use that instead of subclassing. - If the component supports custom initialization, look for an init method that accepts a height parameter (e.g.,
init(height: CGFloat)), which would let you set the height when creating the view controller.
This approach works because the original BottomPopupViewController likely uses the popupHeight property internally to calculate the popup's layout, and overriding it lets you inject your custom value.
内容的提问来源于stack exchange,提问作者user12473575

