Changing the frame of an inputAccessoryView in iOS 8

后端 未结 9 2135
遇见更好的自我
遇见更好的自我 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: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()
            }
        }
    }
    

提交回复
热议问题