Swift inputAccessoryView覆盖错误
我遇到了一个奇怪的错误与我的inputAccessoryView
的外观。 在转型过程中,看起来像这样:
过渡之后,看起来应该如此:
我重写了这样的属性:
override var inputAccessoryView: UIView! { get { if composeView == nil { composeView = CommentComposeView(frame: CGRectMake(0, 0, 0, MinimumToolbarHeight - 0.5)) self.setupSignals() } return composeView } }
我想知道是否有人可以指出我正在做的事情有什么明显的缺陷,或者提供一些关于如何确保我的观点在过渡之前,之中,之后出现的信息。
谢谢!
编辑
这是我的CommentComposeView
:
import UIKit class CommentComposeView: UIToolbar { var textView: SAMTextView! var sendButton: UIButton! private var didSetConstraints: Bool = false required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.initialize() } override init(frame: CGRect) { super.init(frame: frame) self.initialize() } private func initialize() { textView = SAMTextView(frame: CGRectZero) sendButton = UIButton.buttonWithType(.System) as UIButton self.barStyle = .Black self.translucent = true textView.backgroundColor = UIColor.presentOffWhite() textView.font = UIFont.presentLightMedium() textView.layer.borderWidth = 0.5 textView.layer.cornerRadius = 5 textView.placeholder = "Comment" textView.scrollsToTop = false textView.textContainerInset = UIEdgeInsetsMake(4, 3, 3, 3) textView.keyboardAppearance = .Dark textView.keyboardType = .Twitter self.addSubview(textView) sendButton = UIButton.buttonWithType(.System) as UIButton sendButton.enabled = false sendButton.titleLabel!.font = UIFont.presentBoldLarge() sendButton.setTitle("Send", forState: .Normal) sendButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) sendButton.setTitleColor(UIColor.presentCyan(), forState: .Highlighted) sendButton.setTitleColor(UIColor.presentLightGray(), forState: .Disabled) sendButton.contentEdgeInsets = UIEdgeInsetsMake(6, 6, 6, 6) self.addSubview(sendButton) RAC(self.sendButton, "enabled") <~ self.textView.rac_textSignal() .map { text in return (text as NSString).length > 0 } textView.setTranslatesAutoresizingMaskIntoConstraints(false) sendButton.setTranslatesAutoresizingMaskIntoConstraints(false) } override func updateConstraints() { super.updateConstraints() if !didSetConstraints { // TODO: Replace raw constraints with a friendlier looking DSL self.addConstraint( NSLayoutConstraint(item: textView, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: 8) ) self.addConstraint( NSLayoutConstraint(item: textView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 7.5) ) self.addConstraint( NSLayoutConstraint(item: textView, attribute: .Right, relatedBy: .Equal, toItem: sendButton, attribute: .Left, multiplier: 1, constant: -2) ) self.addConstraint( NSLayoutConstraint(item: textView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: -8) ) self.addConstraint( NSLayoutConstraint(item: sendButton, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: 0) ) self.addConstraint( NSLayoutConstraint(item: sendButton, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: -4.5) ) } } }
这是iOS8问题与inputAccessoryView自动布局。 问题是在初始布局期间,UIToolbar的clas _UIToolbarBackground
的子视图没有正确定位。 尝试做下一件事情:
- 使
CommentComposeView
子类化UIView
,而不是UIToolbar
,将UIToolbar
实例添加为子视图。 - 在CommentComposeView中使用自动布局遮罩(不是实际的约束)
- 在您的
-layoutSubviews
中覆盖-layoutSubviews
像这样:
- (void)layoutSubviews { [super layoutSubviews]; contentToolbar.frame = self.bounds; sendButton.frame = CGRectMake(0.f, 0.f, 44.f, self.bounds.size.height); textView.frame = CGRectMake(44.f, 0.f, self.bounds.size.width - 44.f, self.bounds.size.height); }