label showing top of screen instead of being on the inputAccessoryView(标签显示屏幕顶部而不是在 inputAccessoryView)
问题描述
这是我的代码:
var messageView : UITextView = {
var textView = UITextView()
textView.text = " Add your message here"
textView.textColor = UIColor.lightGrayColor()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = UIColor.lightGrayColor()
textView.layer.cornerRadius = 3
textView.clipsToBounds = true
textView.keyboardAppearance = .Dark
textView.layer.borderWidth = 1.0
textView.layer.borderColor = UIColor.lightGrayColor()
textView.autocorrectionType = .no
// MARK: Setup accesorryView
let label = UILabel()
label.text = "You have a 100 character limit"
label.translatesAutoresizingMaskIntoConstraints = false
let accessoryView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 44))
accessoryView.backgroundColor = UIColor.redColor()
accessoryView.addSubview(label)
accessoryView.leadingAnchor.constraintEqualToAnchor(label.leadingAnchor, constant: 18)
accessoryView.centerYAnchor.constraintEqualToAnchor(label.centerYAnchor)
textView.inputAccessoryView = accessoryView
return textView
}()
我正在尝试将 inputAccessoryView 添加到我的 TextView 的键盘.我的 inputAccessoryView 必须有一个标签说你有 100 个字符的限制"...
I'm trying to add an inputAccessoryView to my TextView's keyboard. My inputAccessoryView must have a label saying "You have a 100 character limit"...
但我目前的结果是:
蓝色的文本...正是我想要在 inputAccessoryView 中出现的标签,但它在我的屏幕顶部...
The text in the blue...is exactly the label I want to be in the inputAccessoryView, but it's on the top of my screen...
推荐答案
需要将标签上的translatesAutoresizingMaskIntoConstraints
设置为false
和isActive
到 true
上的约束.基本上你的约束代码应该是这样的:
You need to set translatesAutoresizingMaskIntoConstraints
on the label to false
and isActive
to true
on the constraints. Basically your constrains code should look like this:
accessoryView.leadingAnchor.constraintEqualToAnchor(label.leadingAnchor, constant: 18).isActive = true
accessoryView.centerYAnchor.constraintEqualToAnchor(label.centerYAnchor).isActive = true
这篇关于标签显示屏幕顶部而不是在 inputAccessoryView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:标签显示屏幕顶部而不是在 inputAccessoryView
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01