如何在iOS8中将InputTextView和UIButton添加到导航控制器的工具栏中? [英] How to add InputTextView and UIButton to Navigation Controller's toolbar in iOS8?
问题描述
这是我到目前为止所拥有的:
Here's what I have so far:
toolBar = self.navigationController?.toolbar
textView = InputTextView(frame: CGRectZero)
textView.backgroundColor = UIColor(white: 250/255, alpha: 1)
textView.delegate = self
textView.font = UIFont.systemFontOfSize(messageFontSize)
textView.layer.borderColor = UIColor(red: 200/255, green: 200/255, blue: 205/255, alpha:1).CGColor
textView.layer.borderWidth = 0.5
textView.layer.cornerRadius = 5
//textView.placeholder = "Message"
textView.scrollsToTop = false
textView.textContainerInset = UIEdgeInsetsMake(4, 3, 3, 3)
//textView.inputView = toolBar
//toolBar.addSubview(textView)
sendButton = UIButton.buttonWithType(.System) as UIButton
sendButton.enabled = false
sendButton.titleLabel?.font = UIFont.boldSystemFontOfSize(17)
sendButton.setTitle("Send", forState: .Normal)
sendButton.setTitleColor(UIColor(red: 142/255, green: 142/255, blue: 147/255, alpha: 1), forState: .Disabled)
sendButton.setTitleColor(UIColor(red: 1/255, green: 122/255, blue: 255/255, alpha: 1), forState: .Normal)
sendButton.contentEdgeInsets = UIEdgeInsets(top: 6, left: 6, bottom: 6, right: 6)
sendButton.addTarget(self, action: "sendAction", forControlEvents: UIControlEvents.TouchUpInside)
//toolBar.addSubview(sendButton)
self.setToolbarItems([textView, sendButton], animated: false)
// Auto Layout allows `sendButton` to change width, e.g., for localization.
textView.setTranslatesAutoresizingMaskIntoConstraints(false)
sendButton.setTranslatesAutoresizingMaskIntoConstraints(false)
toolBar.addConstraint(NSLayoutConstraint(item: textView, attribute: .Left, relatedBy: .Equal, toItem: toolBar, attribute: .Left, multiplier: 1, constant: 8))
toolBar.addConstraint(NSLayoutConstraint(item: textView, attribute: .Top, relatedBy: .Equal, toItem: toolBar, attribute: .Top, multiplier: 1, constant: 7.5))
toolBar.addConstraint(NSLayoutConstraint(item: textView, attribute: .Right, relatedBy: .Equal, toItem: sendButton, attribute: .Left, multiplier: 1, constant: -2))
toolBar.addConstraint(NSLayoutConstraint(item: textView, attribute: .Bottom, relatedBy: .Equal, toItem: toolBar, attribute: .Bottom, multiplier: 1, constant: -8))
toolBar.addConstraint(NSLayoutConstraint(item: sendButton, attribute: .Right, relatedBy: .Equal, toItem: toolBar, attribute: .Right, multiplier: 1, constant: 0))
toolBar.addConstraint(NSLayoutConstraint(item: sendButton, attribute: .Bottom, relatedBy: .Equal, toItem: toolBar, attribute: .Bottom, multiplier: 1, constant: -4.5))
return toolBar
现在,没有任何错误,但是InputTextView和UIButton没有出现.通过注释掉您在上面看到的类似于toolBar.addSubview(textView)
的行,我已经能够消除错误.工具栏为空.
Right now, there are no errors but the InputTextView and UIButton do not appear. I've been able to remove errors by commenting out the lines you see above like toolBar.addSubview(textView)
. The toolbar is empty.
推荐答案
您当前的代码有两个问题.
There are two problems with your current code.
(1)textView
和sendButton
都具有CGRectZero
大小的帧.
(1) Both textView
and sendButton
have frames of size CGRectZero
.
(2)您只能将UIBarButtonItem
添加到UIToolbar
;因此,为了将textView
和sendButton
添加到工具栏中,您必须使用initWithCustomView
将它们添加到UIBarButtonItem
中,例如:
(2) You can only add UIBarButtonItem
s to a UIToolbar
; so in order to add your textView
and sendButton
to your toolbar, you have to add them to UIBarButtonItem
s using initWithCustomView
, ex:
toolBar = self.navigationController?.toolbar
textView = InputTextView(frame: CGRectMake(0, 0, 150, 30)) //<-- set frame greater than CGRectZero
textView.backgroundColor = UIColor(white: 250/255, alpha: 1)
textView.delegate = self
textView.font = UIFont.systemFontOfSize(messageFontSize)
textView.layer.borderColor = UIColor(red: 200/255, green: 200/255, blue: 205/255, alpha:1).CGColor
textView.layer.borderWidth = 0.5
textView.layer.cornerRadius = 5
textView.scrollsToTop = false
textView.textContainerInset = UIEdgeInsetsMake(4, 3, 3, 3)
sendButton = UIButton.buttonWithType(.System) as UIButton
sendButton.frame = CGRectMake(0, 0, 60, 30) // <-- add frame (or use sendButton.sizeToFit() once the titleLabel has been added)
sendButton.enabled = false
sendButton.titleLabel?.font = UIFont.boldSystemFontOfSize(17)
sendButton.setTitle("Send", forState: .Normal)
sendButton.setTitleColor(UIColor(red: 142/255, green: 142/255, blue: 147/255, alpha: 1), forState: .Disabled)
sendButton.setTitleColor(UIColor(red: 1/255, green: 122/255, blue: 255/255, alpha: 1), forState: .Normal)
sendButton.contentEdgeInsets = UIEdgeInsets(top: 6, left: 6, bottom: 6, right: 6)
sendButton.addTarget(self, action: "sendAction", forControlEvents: UIControlEvents.TouchUpInside)
let textViewItem = UIBarButtonItem(customView: textView)
let sendButtonItem = UIBarButtonItem(customView: sendButton)
self.setToolbarItems([textViewItem, sendButtonItem], animated: false)
这篇关于如何在iOS8中将InputTextView和UIButton添加到导航控制器的工具栏中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!