作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 UIView 子类,它有一个用于标题的 UILabel、一个按钮和另一个 subview ,当该 View 在另一个 View Controller 中使用时,该 subview 将根据传递的值进行更改。
class CustomView: UIView {
enum PickerType {
case pikerView
case datePicker
}
private var dialogView: UIView!
private var titleLabel: UILabel!
private var pickerView: UIPickerView!
private var datePicker: UIDatePicker!
private var pickerType: PickerType!
init() {
super.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
setupView()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupView() {
self.dialogView = createContainerView()
self.dialogView.layer.shouldRasterize = true
self.dialogView.layer.rasterizationScale = UIScreen.mainScreen().scale
self.layer.shouldRasterize = true
self.layer.rasterizationScale = UIScreen.mainScreen().scale
self.dialogView.layer.opacity = 0.5
self.dialogView.layer.transform = CATransform3DMakeScale(1.3, 1.3, 1)
self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
self.addSubview(self.dialogView)
}
private func createContainerView() -> UIView {
let screenSize = getScreenSize()
let dialogSize = CGSizeMake(screenSize.width, 300)
// For the black background
self.frame = CGRectMake(0, 0, screenSize.width, screenSize.height)
let dialogContainer = UIView(frame: CGRectMake(0, screenSize.height - dialogSize.height, dialogSize.width, dialogSize.height))
//Title
self.titleLabel = UILabel(frame: CGRectMake(10, 10, dialogSize.width - 20, 30))
self.titleLabel.textAlignment = NSTextAlignment.Center
self.titleLabel.font = UIFont.boldSystemFontOfSize(17)
dialogContainer.addSubview(self.titleLabel)
//Add the PickerView
addPickerView(dialogContainer)
return dialogContainer
}
/**Add the appropriate view based on pickerType*/
private func addPickerView(dialogContainer: UIView) {
let screenSize = countScreenSize()
let dialogSize = CGSizeMake(screenSize.width, 250)
if pickerType == PickerType.pikerView {
self.pickerView = UIPickerView(frame: CGRectMake(10, 30, 0, 0))
self.pickerView.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin
self.pickerView.frame.size.width = dialogSize.width - 20
dialogContainer.addSubview(self.pickerView)
} else if pickerType == PickerType.datePicker {
self.datePicker = UIDatePicker(frame: CGRectMake(10, 30, 0, 0))
self.datePicker.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin
self.datePicker.frame.size.width = dialogSize.width - 20
dialogContainer.addSubview(self.datePicker)
}
}
func showPickerView(title: String) {
self.pickerType = PickerType.pikerView
self.titleLabel.text = title
}
func showDatePicker(title: String) {
self.pickerType = PickerType.datePicker
self.titleLabel.text = title
}
}
这个想法是,如果调用 CustomView().showPickerView,则 pickerType 变量将被分配适当的值并加载 UIPickerView。如果调用 CustomView().showDatePicker,则会加载 UIDatePicker。
我的问题是,在调用 showPickerView() 或 showDatePicker() 之前调用 addPickerView。
如何在 View 初始化之前传递 pickerType 的值?
最佳答案
我通过将初始值设定项更改为:
init(picker: PickerType) {
super.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
self.pickerType = picker
setupView()
}
并在 CustomView 之外取出 PickerType 枚举。
现在我可以使用自定义 View :
CustomView(picker: PickerType.datePicker).showDatePicker("Select date")
CustomView(picker: PickerType.pickerView).showPickerView("Select data")
但如果有更有效的方法来实现这一点,我希望得到建议。
关于ios - 带有来自传递值的可选 SubView 的 swift UIView 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38287423/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!