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


基础教程推荐
- 如何使用 YouTube API V3? 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01