使用CocoaPod设置popupHeight提示只读属性,寻求解决方案
Hey there! I’ve run into similar read-only property headaches with CocoaPods before—let’s break down how to solve this for your BottomPopupViewController setup.
Why You’re Seeing This Error
The popupHeight property is marked as read-only in the framework’s code (meaning it only has a getter { get } and no setter). This is usually intentional: the framework likely expects you to set the height through a different method or configuration pattern instead of directly assigning the property.
Practical Solutions to Try
1. Check for a Custom Initializer That Accepts Height
Many popup frameworks provide custom initializers that let you pass the height upfront. If BottomPopupViewController has one, use it instead of storyboard instantiation (or combine both if possible):
// If a custom initializer exists: let popupVC = BottomPopupViewController(popupHeight: 50.0) popupVC.popupDelegate = self present(popupVC, animated: true, completion: nil)
If you need to stick with storyboard instantiation, check if the framework supports setting height via user-defined runtime attributes in Interface Builder.
2. Subclass and Override the Read-Only Property
If the class is subclassable, create a custom subclass where you can control the height with a stored property:
class CustomBottomPopupVC: BottomPopupViewController { // Add your own stored property to control height var customPopupHeight: CGFloat = 50.0 // Override the read-only popupHeight to return your custom value override var popupHeight: CGFloat { return customPopupHeight } }
Then update your storyboard’s view controller class to CustomBottomPopupVC, and adjust your code:
@IBAction func testBtnPressed(_ sender: Any) { guard let popupVC = storyboard?.instantiateViewController(withIdentifier: "popup") as? CustomBottomPopupVC else { return } popupVC.customPopupHeight = 50.0 // Now this works! popupVC.popupDelegate = self present(popupVC, animated: true, completion: nil) }
3. Use a Related Property (Like preferredContentSize)
Some frameworks tie the popup height to the view controller’s preferredContentSize. Give this a shot:
popupVC.preferredContentSize = CGSize(width: view.bounds.width, height: 50.0)
4. Check the Framework’s Documentation/Demo
Take a quick look at the CocoaPod’s README or included demo project. Most frameworks show explicit examples of setting popup dimensions—you might find that the height is determined by the content view’s size instead of a direct property.
Final Notes
If none of these work, check the framework’s GitHub Issues page to see if other users have faced the same problem, or reach out to the maintainer for clarification.
内容的提问来源于stack exchange,提问作者user12473575

