作为iOS开发的相对noob,我很难让PHFComposeBarView控件在我的Swift iOS应用程序中变得活跃。
在[{3}}和PHFComposeBarView example app中眯着眼睛看着Obj-C,我尽力将基本设置翻译成Swift,以便控件显示在主视图中:
但是,我还没有设法让控件以编程方式变为活动状态。
我在Swift 2.0 / XCode 7.0 Beta 2中创建了一个this SO answer,其ViewController如下所示:
ViewController.swift:
import UIKit
import PHFComposeBarView
class ViewController: UIViewController, PHFComposeBarViewDelegate {
var composeBar: PHFComposeBarView {
let viewBounds = self.view.bounds
let frame = CGRectMake(0.0, viewBounds.size.height - PHFComposeBarViewInitialHeight, viewBounds.size.width, PHFComposeBarViewInitialHeight)
let composeBarView = PHFComposeBarView(frame: frame)
composeBarView.delegate = self
return composeBarView
}
override var inputAccessoryView: UIView {
return self.composeBar
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override func viewDidAppear(animated: Bool) {
print("viewDidAppear self \(self)")
print("composeBar.delegate \(self.composeBar.delegate)")
print("composeBar.textView.delegate \(self.composeBar.textView.delegate)")
print("composeBar.canBecomeFirstResponder() \(self.composeBar.canBecomeFirstResponder())")
print("composeBar.textView.canBecomeFirstResponder() \(self.composeBar.textView.canBecomeFirstResponder())")
let res = self.composeBar.becomeFirstResponder()
print("composeBar.becomeFirstResponder() \(res)")
print("composeBar.isFirstResponder() \(self.composeBar.isFirstResponder())")
print("composeBar.textView.isFirstResponder() \(self.composeBar.textView.isFirstResponder())")
}
}
控制台:
viewDidAppear self <SwiftPHFComposeBarTest.ViewController: 0x7fe088619d90>
composeBar.delegate Optional(<SwiftPHFComposeBarTest.ViewController: 0x7fe088619d90>)
composeBar.textView.delegate Optional(<PHFDelegateChain: 0x7fe088654850>)
composeBar.canBecomeFirstResponder() true
composeBar.textView.canBecomeFirstResponder() true
composeBar.becomeFirstResponder() false
composeBar.isFirstResponder() false
composeBar.textView.isFirstResponder() false
composeBar.canBecomeFirstResponder()
如何返回true
但composeBar.becomeFirstResponder()
返回false
怎么办?
另外,我不确定该委托是否是一个问题。
编辑:已更新以包含调试输出。
答案 0 :(得分:1)
好吧,一个解决方案似乎是use the storyboard instead:
ViewController.swift:
import UIKit
import PHFComposeBarView
class ViewController: UIViewController, PHFComposeBarViewDelegate {
@IBOutlet weak var composeBarView: PHFComposeBarView!
override func viewDidLoad() {
super.viewDidLoad()
composeBarView.delegate = self
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override var inputAccessoryView: UIView {
composeBarView.removeFromSuperview()
return composeBarView
}
}