2015-05-22 21 views
9

Về cơ bản tôi đang cố gắng tạo một UITextView thay đổi kích thước trong một thuộc tính inputAccessoryView.Thay đổi kích thước inputAccessoryView động trong iOS 8

Tôi có chế độ xemController với phương thức canBecomeFirstResponder trả về true và chế độ xem tôi khởi tạo qua lớp tùy chỉnh (nhận được từ XIB). Trong chế độ xem này là một UITextView nằm.

Tôi cố gắng thay đổi kích thước đầu vào đầy đủAccessoryView từ bên trong lớp đó. Tôi đã thử theo một vài cách: đặt khung trực tiếp, cố gắng sử dụng giới hạn chiều cao. Có vẻ nó thay đổi kích thước chỉ bằng một nửa chiều:

này về cơ bản là những gì tôi muốn (có hoặc không có autolayout, nhưng làm việc trong iOS 7/8 +):

class MessageInputAccessory: UIView, UITextViewDelegate 
{ 
    @IBOutlet weak var textView: UITextView! 

    func textViewDidChange(textView: UITextView) 
    { 
     var contentSize = textView.sizeThatFits(CGSizeMake(textView.frame.size.width, CGFloat.max)) 

     self.frame.size.height = contentSize.height + 20 

     self.textView.reloadInputViews() 
    } 
} 

Tôi đã tìm thấy bài đăng này: Changing the frame of an inputAccessoryView in iOS 8 . Nói rằng một ràng buộc được tạo tự động đang được tạo ra. Vấn đề là, nó không tạo ra ràng buộc đối với tôi. Không có cách nào, với tính năng tự động tắt hoặc bật tính năng tự động điền (cũng thông qua setTranslatesAutoresizingMaskIntoConstraints()).

Trả lời

7

Nó làm việc cho tôi trong iOS7/iOS8:

class MessageInputAccessory: UIView, UITextViewDelegate { 
    private var textView: UITextView! 
    private var heightConstraint: NSLayoutConstraint? 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     commonInit() 
    } 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 

     commonInit() 
    } 

    func commonInit() { 
     self.userInteractionEnabled = true 

     textView = UITextView() 
     textView.delegate = self 
     textView.bounces = false 
     textView.scrollEnabled = false 
     textView.layer.cornerRadius = 5 
     textView.layer.borderWidth = 1 
     textView.layer.borderColor = UIColor.blueColor().CGColor 

     self.addSubview(textView) 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     textView.frame = self.bounds 
    } 

    override func addConstraint(constraint: NSLayoutConstraint) { 
     self.heightConstraint = constraint 

     super.addConstraint(constraint) 
    } 

    func textViewDidChange(textView: UITextView) { 
     var contentSize = textView.sizeThatFits(CGSizeMake(textView.frame.size.width, CGFloat.max)) 
     self.frame.size.height = contentSize.height 

     if let heightConstraint = self.heightConstraint { 
      heightConstraint.constant = self.frame.size.height 
     } 

     self.textView.reloadInputViews() 
    } 
} 

EDIT: này làm việc với xib quá (iOS7/iOS8):

class MessageInputAccessory: UIView, UITextViewDelegate { 
    @IBOutlet var textView: UITextView! 
    @IBOutlet var textViewHeightConstraint: NSLayoutConstraint! 
    private var heightConstraint: NSLayoutConstraint? 

    override func awakeFromNib() { 
     textView.layer.cornerRadius = 5 
     textView.layer.borderWidth = 1 
     textView.layer.borderColor = UIColor.blueColor().CGColor 
    } 

    override func layoutSubviews() { 
     textViewHeightConstraint.constant = self.bounds.size.height 

     super.layoutSubviews() 
    } 

    override func addConstraint(constraint: NSLayoutConstraint) { 
     if constraint.firstItem === self { 
      self.heightConstraint = constraint 
     } 

     super.addConstraint(constraint) 
    } 

    override func addConstraints(constraints: [AnyObject]) { 
     super.addConstraints(constraints) 
    } 

    func textViewDidChange(textView: UITextView) { 
     var contentSize = textView.sizeThatFits(CGSizeMake(textView.frame.size.width, CGFloat.max)) 
     self.frame.size.height = contentSize.height 

     if let heightConstraint = self.heightConstraint { 
      heightConstraint.constant = self.frame.size.height 
     } 

     self.textView.reloadInputViews() 
    } 

    override func intrinsicContentSize() -> CGSize { 
     return self.bounds.size; 
    } 
} 

Có tôi xib: enter image description here

Nó không phải là một giải pháp rất tốt, nhưng nó hoạt động .. Xin vui lòng cho tôi biết nếu bạn biết một cách tốt hơn.

+0

Tác vụ này thực sự, nhưng trong trường hợp của tôi, tôi tải chế độ xem từ một NIB. Tôi thực sự muốn tải chế độ xem từ tệp NIB thay vì tạo chế độ xem theo chương trình. – Wesley

+0

@Wesley Tôi đã chỉnh sửa. xin vui lòng cho tôi biết nếu nó hoạt động – Bannings

+0

Có nó hoạt động, nó thực sự sucks bạn phải hack xung quanh thiết lập chiều cao với một hạn chế riêng! Tôi sẽ chấp nhận nó là chính xác cho bây giờ, có lẽ có một giải pháp tốt hơn cho nó. – Wesley

-1

Sửa lỗi khá đơn giản là không làm thay đổi kích thước của inputAccessoryView thực tế - nếu bạn biết chiều cao tối đa bạn cần, bạn có thể tạo chiều cao tối đa, làm cho nó trong suốt và thêm subview thay đổi kích thước tuy nhiên bạn thích.

Các chi tiết mà bạn cần để làm cho nó làm việc là rằng inputAccessoryView cần phải là một phân lớp UIView chứa sau:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    point = [self.innerView convertPoint:point fromView:self]; 
    return [self.innerView pointInside:point withEvent:event]; 
} 

(nơi self.innerView là subview động thay đổi kích thước của bạn).

Điều này làm cho nó xác nhận quyền sở hữu các vòi bên trong chế độ xem phụ, nhưng vượt qua bất kỳ thao tác nào nằm ngoài nó.

Hạn chế chính cần lưu ý là thông báo bàn phím sẽ cung cấp cho bạn khung chứa toàn bộ đầu vào chiều cao tối đaAccessoryView. Vì vậy, nếu bạn đang sử dụng chúng để (ví dụ) điều chỉnh insets, bạn cần phải làm thêm một số toán học ở đó.

Các vấn đề liên quan