Changing the frame of an inputAccessoryView in iOS 8

后端 未结 9 2119
遇见更好的自我
遇见更好的自我 2020-12-13 02:42

Long time lurker - first time poster!

I am having an issue while recreating a bar with a UITextView like WhatsApp does it.

I am using a custom <

相关标签:
9条回答
  • 2020-12-13 03:11

    Unfortunately, iOS8 adds a private height constraint to the inputAccessoryView, and this constraint is not public.

    I recommend recreating the accessory view when its frame should change, and call reloadInputViews so that the new one is installed.

    This is what I do, and it works as expected.

    0 讨论(0)
  • 2020-12-13 03:16

    I've been banging my head against the wall on this one for quite some time, as the behavior changed from iOS 7 to iOS 8. I tried everything, until the most obvious solution of all worked for me:

    inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    

    duh!

    0 讨论(0)
  • 2020-12-13 03:18

    Here's a complete, self-contained solution (thanks @JohnnyC and @JoãoNunes for pointing me in the right direction, @stigi for explaining how to animate intrinsicContent changes):

    class InputAccessoryView: UIView {
    
        // InputAccessoryView is instantiated from nib, but it's not a requirement
        override func awakeFromNib() {
            super.awakeFromNib()        
            autoresizingMask = .FlexibleHeight
        }
    
        override func intrinsicContentSize() -> CGSize {
            let exactHeight = // calculate exact height of your view here
            return CGSize(width: UIViewNoIntrinsicMetric, height: exactHeight)
        }
    
        func somethingDidHappen() {
            // invalidate intrinsic content size, animate superview layout        
            UIView.animateWithDuration(0.2) {
                self.invalidateIntrinsicContentSize()
                self.superview?.setNeedsLayout()
                self.superview?.layoutIfNeeded()
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题