ios - Swift inputAccessoryView 覆盖错误

标签 ios swift inputaccessoryview

我的 inputAccessoryView 的外观遇到了奇怪的错误。在转换过程中,它看起来像这样:

Mid-transition

转换后,它看起来应该是这样:

enter image description here

我像这样重写该属性:

    override var inputAccessoryView: UIView! {
    get {
        if composeView == nil {
            composeView = CommentComposeView(frame: CGRectMake(0, 0, 0, MinimumToolbarHeight - 0.5))
            self.setupSignals()
        }

        return composeView
    }
}

我想知道是否有人可以指出我正在做的事情中的任何明显缺陷,或者提供更多关于如何确保我的 View 在转换之前、期间和之后显示应有的信息。

谢谢!

编辑

这是我的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 类 _UIToolbarBackground 的 subview 在初始布局期间未正确定位。尝试做接下来的事情:

  1. 使 CommentComposeView 子类化 UIView,而不是 UIToolbar,添加 UIToolbar 实例作为 subview 。
  2. CommentComposeView 中使用自动布局蒙版(而非实际约束)
  3. CommentComposeView 中覆盖 -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);
} 

关于ios - Swift inputAccessoryView 覆盖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25188392/

相关文章:

c++ - 高级 iOS GUI(最好使用 C++)

swift - 如何在 Xcode 中将 Swift 版本从 5 更改为 4?

ios - 使用 Xamarin 在键盘上方添加自定义 View

ios - 防止键盘输入附件 View 引起的表格 View 动画

ios - 如何安装 SDWebImage

ios - 需要有关 Swift while 语句的建议

ios - Cocos2d 雪碧连续

ios - 在任何文本字段中键入会使应用程序崩溃

ios - 循环结束后如何调用函数?

ios11 - 如何在 inputAccessoryView swift 4/ios 11/xcode 9 的底部添加安全区域约束