Swift inputAccessoryView覆盖错误 [英] Swift inputAccessoryView override bug

查看:134
本文介绍了Swift inputAccessoryView覆盖错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的inputAccessoryView出现了一个奇怪的错误.在过渡过程中,它看起来像这样:

I'm experiencing a weird bug with the appearance of my inputAccessoryView. While in the middle of a transition, it appears like so:

过渡后,它应显示为:

我这样覆盖属性:

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

        return composeView
    }
}

我想知道是否有人可以指出我正在做的任何明显的缺陷,或者提供更多有关如何确保我的视图在过渡之前,过渡期间和过渡之后显示的信息.

I'm wondering if anyone can point out any obvious flaw in what I'm doing or provide some more information on how to ensure my view appears as it should, before, during, and after transitions.

谢谢!

编辑

这是我的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的子视图未正确放置.尝试做以下事情:

This is iOS8 issue with inputAccessoryView autolayout. Issue is that UIToolbar's subview of clas _UIToolbarBackground is not positioned properly during initial layout. Try to do next things:

  1. CommentComposeView子类化为UIView,而不是UIToolbar,将UIToolbar的实例添加为子视图.
  2. CommentComposeView
  3. 内使用自动布局掩码(不是实际约束)
  4. 像这样在您的CommentComposeView中覆盖-layoutSubviews:
  1. Make CommentComposeView subclassing UIView, not UIToolbar, add instance of UIToolbar as subview.
  2. Use autolayout masks (not actual constraints) inside your CommentComposeView
  3. Override -layoutSubviews in your CommentComposeView like this:

- (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);
} 

这篇关于Swift inputAccessoryView覆盖错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆