- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试以编程方式创建键盘附件 View 。我已经设置了一个容器 View ,并在其中尝试设置一个文本字段、发布按钮和一个表情符号。这是我正在尝试制作的示例。
这是我正在使用的代码。我认为问题出在我设置约束时。我脑海中闪过几个问题:
我需要为容器 View 设置约束吗?
如何向文本字段添加适当的约束?
override var inputAccessoryView: UIView? {
get {
//Set up the container
let containerView = UIView()
containerView.backgroundColor = #colorLiteral(red: 0.9784782529, green: 0.9650371671, blue: 0.9372026324, alpha: 1)
containerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 60)
let textField = UITextField()
textField.placeholder = "Add a reframe..."
textField.isSecureTextEntry = false
textField.textAlignment = .left
textField.borderStyle = .none
textField.translatesAutoresizingMaskIntoConstraints = false
textField.translatesAutoresizingMaskIntoConstraints = false
textField.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
textField.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
textField.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
textField.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
textField.heightAnchor.constraint(equalToConstant: 50)
containerView.addSubview(textField)
return containerView
}
}
这是我不断遇到的错误。
*** 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用 anchor 激活约束,因为它们没有共同的祖先。约束或其 anchor 是否引用不同 View 层次结构中的项目?这是非法的。”
编辑:
查看帖子 Controller
lazy var containerView: CommentInputAccessoryView = {
let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 60)
let commentInputAccessoryView = CommentInputAccessoryView(frame: frame)
commentInputAccessoryView.delegate = self
return commentInputAccessoryView
}()
//Setting up the keyboard accessory view for comments.
override var inputAccessoryView: UIView? {
get {
return containerView
}
}
override var canBecomeFirstResponder: Bool {
return true
}
评论输入附件 View
protocol CommentInputAccessoryViewDelegate {
func didSend(for comment: String)
}
class CommentInputAccessoryView: UIView {
var delegate: CommentInputAccessoryViewDelegate?
func clearCommentTextView() {
commentTextView.text = nil
showPlaceholderLabel()
}
let commentTextView: UITextView = {
let text = UITextView()
text.translatesAutoresizingMaskIntoConstraints = false
//text.placeholder = "Add a reframe..."
text.textAlignment = .left
text.backgroundColor = #colorLiteral(red: 0.9784782529, green: 0.9650371671, blue: 0.9372026324, alpha: 1)
text.layer.cornerRadius = 50/2
text.layer.masksToBounds = true
text.isScrollEnabled = false
text.font = UIFont.systemFont(ofSize: 16)
text.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 64)
//text.borderStyle = .none
return text
}()
let placeholderLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Add a response..."
label.textColor = .black
label.font = UIFont.systemFont(ofSize: 16)
return label
}()
func showPlaceholderLabel() {
placeholderLabel.isHidden = false
}
let sendButton: UIButton = {
let send = UIButton(type: .system)
//let sendButton = UIImageView(image: #imageLiteral(resourceName: "arrowUp"))
send.translatesAutoresizingMaskIntoConstraints = false
send.setTitle("Send", for: .normal)
send.setTitleColor(#colorLiteral(red: 0.2901960784, green: 0.3725490196, blue: 0.937254902, alpha: 1), for: .normal)
send.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
send.addTarget(self, action: #selector(handlePostComment), for: .touchUpInside)
return send
}()
let hugButton: UIButton = {
let hug = UIButton()
hug.translatesAutoresizingMaskIntoConstraints = false
hug.setTitle("🤗", for: .normal)
hug.backgroundColor = #colorLiteral(red: 0.9784782529, green: 0.9650371671, blue: 0.9372026324, alpha: 1)
hug.contentEdgeInsets = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
hug.layer.cornerRadius = 25
hug.layer.masksToBounds = true
return hug
}()
override init(frame: CGRect) {
super.init(frame: frame)
autoresizingMask = .flexibleHeight
addSubview(commentTextView)
addSubview(sendButton)
addSubview(hugButton)
addSubview(placeholderLabel)
if #available(iOS 11.0, *) {
commentTextView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
hugButton.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
}
else {
}
commentTextView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 8).isActive = true
commentTextView.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
commentTextView.trailingAnchor.constraint(equalTo: hugButton.leadingAnchor, constant: 8)
placeholderLabel.leadingAnchor.constraint(equalTo: commentTextView.leadingAnchor, constant: 18).isActive = true
placeholderLabel.centerYAnchor.constraint(equalTo: self.commentTextView.centerYAnchor).isActive = true
sendButton.trailingAnchor.constraint(equalTo: self.commentTextView.trailingAnchor, constant: -10).isActive = true
sendButton.centerYAnchor.constraint(equalTo: self.commentTextView.bottomAnchor, constant: -22).isActive = true
hugButton.leadingAnchor.constraint(equalTo: self.commentTextView.trailingAnchor, constant: 10).isActive = true
hugButton.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -8).isActive = true
hugButton.widthAnchor.constraint(equalToConstant: 40)
//hugButton.centerYAnchor.constraint(equalTo: self.commentTextView.centerYAnchor).isActive = true
NotificationCenter.default.addObserver(self, selector: #selector(handleTextChanged), name: .UITextViewTextDidChange, object: nil)
}
override var intrinsicContentSize: CGSize {
return .zero
}
@objc func handleTextChanged() {
placeholderLabel.isHidden = !self.commentTextView.text.isEmpty
}
@objc func handlePostComment() {
guard let commentText = commentTextView.text else {return}
delegate?.didSend(for: commentText)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这里有一些照片可能有助于了解正在发生的事情。
InputAccessoryView 工作: Click here
TextView 扩展: Click here.
最佳答案
在应用约束 View 之前, View 应该在 View 层次结构中,否则将引发错误。要消除错误,只需在 let textField = UITextField()
之后执行 containerView.addSubview(textField)
。
关于您发布的示例图片,最初的解决方案可能是这样的
override var inputAccessoryView: UIView? {
get {
//Set up the container
let containerView = UIView()
containerView.backgroundColor = #colorLiteral(red: 0.9784782529, green: 0.9650371671, blue: 0.9372026324, alpha: 1)
containerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 60)
let textField = UITextField()
containerView.addSubview(textField)
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "Add a reframe..."
textField.textAlignment = .left
textField.backgroundColor = .white
textField.layer.cornerRadius = 50/2
textField.layer.masksToBounds = true
textField.borderStyle = .none
textField.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 8).isActive = true
textField.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -5).isActive = true
textField.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 10).isActive = true
textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: textField.frame.height)) // adding left padding so it's not sticked to border
textField.leftViewMode = .always
let arrow = UIImageView(image: #imageLiteral(resourceName: "arrowUp"))
containerView.addSubview(arrow)
arrow.translatesAutoresizingMaskIntoConstraints = false
arrow.trailingAnchor.constraint(equalTo: textField.trailingAnchor, constant: -10).isActive = true
arrow.centerYAnchor.constraint(equalTo: textField.centerYAnchor).isActive = true
let button = UIButton()
containerView.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("🤗", for: .normal)
button.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
button.leadingAnchor.constraint(equalTo: textField.trailingAnchor, constant: 10).isActive = true
button.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -8).isActive = true
button.centerYAnchor.constraint(equalTo: textField.centerYAnchor).isActive = true
// Negative values for constraints can be avoided if we change order of views when applying constrains
// f.e. instead of button.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -8).isActive = true
// write containerView.trailingAnchor.constraint(equalTo: button.trailingAnchor, constant: 8).isActive = true
return containerView
}
}
关于ios - 以编程方式创建输入附件 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50141437/
我没有上传附件以使浏览器正常工作。 一些提示是 here , 其他 there . The docs非常好,但我无法将其转换为 AJAX 上传。 我正在寻找一个 super 简单的 HTML/Java
我有一个应用程序可以收集一些信息并允许用户使用 Android 的 Intent 框架共享这些信息。 到目前为止,它以纯文本形式共享报告:使用 putExtra(Intent.EXTRA_TEXT,
我正在尝试通过我的应用程序发布带有附件图片的消息,所以我使用了以下代码: function yamPost(mytoken) { var msg_Body = jQuery("#myB
我在用户表中使用了多对多关系来使登录用户关注另一个用户,但我自己没有弄清楚,我检查了其他人做了什么,并尝试做类似的事情,并且它有效。在我的方法中,我有: function follow(User $u
我正在用 PHP 创建脚本,其作用是将 IMAP 服务器备份到 MySQL 数据库。 我现在的问题是: 如果电子邮件有附件,附件是嵌入在电子邮件本身中还是服务器上的一个单独文件? 我问的原因是: 我可
我正在使用 RavenDB,在我处理任何附件之前删除了一些带有附件的测试文档,所以我在想它们是否还在磁盘上的某个地方,以及如何轻松地找到它们?。 另一个问题是:当文档被删除时,它有一个附件,附件会被自
当您使用 ACTION_SEND Intent (使用额外的 EXTRA_STREAM)将文件附加到电子邮件时,电子邮件应用程序是否将该附加文件复制到它自己的位置?我的应用程序创建了一个文件并将其附加
所以: // Setup mail class, recipients and body $mailer->AddAttachment('/home/mywebsite/public_html/fil
您好,我需要一个 DnD 解决方案来将 Outlook 邮件附件拖到 Stackpane。 JavaFX/展望 2010 stackpaneDragAndDropZone.setOnDragO
我尝试制作一个 PhpSpreadsheet 文档,然后将他添加到邮件附件中。也许是太热了,但在 phpSpreadsheet 文档中几个小时后,我还没有找到任何东西。 这是我发送邮件的文件 $nam
有什么方法可以动画删除 UITableView 单元格附件吗? 我当前正在显示一个 UITableViewCellAccessoryDisclosureIndicator,但我想在所有可见表格单元格上
我正在编写一个 iPhone 应用程序,它要求我以编程方式发送电子邮件附件。附件是我通过代码创建的 csv 文件。然后,我将文件附加到电子邮件中,附件就会显示在手机上。但是,当我向自己发送电子邮件时,
我正在尝试通过收件箱中的名称“MacroEnabled”访问子文件夹,找到其中的所有附件并将它们保存到本地驱动器。 我使用此代码创建一个名为“Documents”的文件夹并保存附件。然而,在进行第二次
将 corda 升级到版本 4 后,我收到 net.corda.core.transactions.MissingContractAttachments:找不到 com.template.contra
我正在尝试让 Jenkins 将一个或一组文件附加到作业已完成的电子邮件通知中。我不断收到以下错误: 发送电子邮件以触发:成功错误:访问要附加的文件时出错:需要 Ant GLOB 模式,但看到 C:\
我创建了一个由来自 mysql 的数据填充的 UITableView(使用 NSJSONSERIALIZATION)。现在问题是一回事。我检索到的是产品名称。我想要一个附件 View (像单元格右侧的
我开发了一个 Java 客户端应用程序,用于下载我自己的电子邮件。我发现我无法在电子邮件中找到某些附件,特别是当我向经过认证的公司发送电子邮件时收到的作为收据的 XML 文件。我用于下载附件的代码:
我正在将我的 sqlite 数据库转换为 Couchdb。我可以转换数据库并上传到 Couchdb 服务器。除了图像之外的一切。我想将图像作为独立附件上传,我想使用 javascript、REST 和
我编写了一段代码,以便能够启动默认的电子邮件服务提供商,即我的 Outlook。这是我的代码: if(role.getValue().equals("1")) { Desktop desktop =
我正在尝试使用链接上共享的代码使用 python 从 Gmail 下载电子邮件附件 https://gist.github.com/baali/2633554 我想应用时间过滤器+主题过滤器并下载附件
我是一名优秀的程序员,十分优秀!