ios - 在 Swift 中的 UITextView 上放置阴影
问题描述
我搜索了一段时间,但无法弄清楚如何实施我找到的解决方案。
下面的帖子定义了我真正想要的东西,它也有一个解决方案,但在 Objective-c 中。我尝试实施该解决方案,但在实施后开始不显示文本。
class AggrementView: UIView, UITextViewDelegate {
let acceptBtn = RippleButton()
let kvkkTextView = UITextView()
private let containerView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
setupConstraints()
}
public convenience init(text: String, imageName: String){
self.init()
}
public convenience init(text: String, imageName: String, senderColor: UIColor){
self.init()
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupUI(){
self.backgroundColor = .clear
acceptBtn.translatesAutoresizingMaskIntoConstraints = false
//acceptBtn.backgroundColor = UIColor(red:0.40, green:0.69, blue:0.07, alpha:1.0)
acceptBtn.backgroundColor = UIColor.gray
acceptBtn.layer.cornerRadius = 6.0
acceptBtn.setTitle("Onaylıyorum", for: UIControlState.normal)
acceptBtn.titleLabel?.font = UIFont(name: "Corbert-Regular", size: 20)!
acceptBtn.setTitleColor(UIColor.flatWhite, for: UIControlState.normal)
//acceptBtn.setAllSideShadow(shadowShowSize: 8.0)
acceptBtn.accessibilityIdentifier = "acceptBtn"
acceptBtn.isEnabled = false
acceptBtn.setTitleColor(UIColor.lightGray, for: .disabled)
kvkkTextView.font = UIFont(name: "Corbert-Regular", size: 15)!
kvkkTextView.setContentOffset(CGPoint.zero, animated: false)
kvkkTextView.layer.cornerRadius = 6.0
kvkkTextView.backgroundColor = .clear
kvkkTextView.layer.borderColor = UIColor.flatGray.cgColor
kvkkTextView.textColor = UIColor.flatWhite
kvkkTextView.delegate = self
kvkkTextView.isEditable = false
containerView.backgroundColor = .clear
containerView.layer.cornerRadius = 6.0
self.addSubview(containerView)
containerView.addSubview(acceptBtn)
containerView.addSubview(kvkkTextView)
}
private func setupConstraints(){
containerView.anchor(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 90, leftConstant: 10, bottomConstant: 20, rightConstant: 10, widthConstant: 0, heightConstant: 0)
kvkkTextView.anchor(self.containerView.topAnchor, left: self.containerView.leftAnchor, bottom: self.acceptBtn.topAnchor, right: self.containerView.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 20, rightConstant: 0, widthConstant: 0, heightConstant: 0)
acceptBtn.anchor(nil, left: self.containerView.leftAnchor, bottom: self.containerView.bottomAnchor, right: self.containerView.rightAnchor, topConstant: 0, leftConstant: 10, bottomConstant: 10, rightConstant: 10, widthConstant: 0, heightConstant: 60)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
print( "View scrolled to the bottom" )
UIView.animate(withDuration: 0.5) {
self.acceptBtn.backgroundColor = UIColor(red:0.40, green:0.69, blue:0.07, alpha:1.0)
self.acceptBtn.isEnabled = true
}
}
let color = UIColor.lightGray
guard
let verticalIndicator = scrollView.subviews.last as? UIImageView,
verticalIndicator.backgroundColor != color,
verticalIndicator.image?.renderingMode != .alwaysTemplate
else { return }
verticalIndicator.layer.masksToBounds = true
verticalIndicator.layer.cornerRadius = verticalIndicator.frame.width / 2
verticalIndicator.backgroundColor = color
verticalIndicator.image = verticalIndicator.image?.withRenderingMode(.alwaysTemplate)
verticalIndicator.tintColor = .clear
}
图片参考:@user2213271 在UITextView 底部阴影中的文字帖子
这是我尝试过的梯度代码:
func applyShadow(){
var layerWidth: CGFloat = 8.0
var gl1 = CAGradientLayer()
var col1 = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0).cgColor
var col2 = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0).cgColor
gl1.colors = [col2, col1, col1, col2]
gl1.locations = [0.0, 0.0, 0.85, 1.0]
//underTextView - this is my UIView
gl1.frame = CGRect(x: 0, y: 0, width: underTextView.frame.width - layerWidth, height: underTextView.frame.height)
//layer for scrollIndicator - it must be visible always good
var gl2 = CAGradientLayer()
gl2.colors = [col1, col1]
gl2.locations = [0.0, 1.0]
gl2.frame = CGRect(x: underTextView.frame.width - layerWidth, y: 0, width: layerWidth, height: underTextView.frame.height)
//create main layer
var gl = CAGradientLayer()
gl.addSublayer(gl1)
gl.addSublayer(gl2)
//add to mask of UIView
underTextView.layer.mask = gl
}
解决方案
您似乎忘记kvkkTextView.translatesAutoresizingMaskIntoConstraints = false
在 setkvkkTextView
的锚点之前添加。所以你kvkkTextView
不能从约束中得到它的框架。
推荐阅读
- bash - 使用每个文件中第一个序列的 ID 自动重命名 fasta 文件
- vue.js - 多条嵌套路由在第 4 次中断
- javascript - 为什么第一次网络调用比后续调用花费更多时间?
- angular - 源可观察对象完成时如何取消订阅内部可观察对象
- c# - 为什么我的播放器在 Z = 990 处停止随机前进
- java - 无法在 Eclipse 的 Maven 中获取“Gluon Glisten”依赖库
- go - Knative pod http 请求
- docker - 在 docker-compose.yml 中定义局部变量?
- mysql - 用另一个表的列更新一个表的一列(一对多关系)
- javafx - 如何将多个控制器连接到一个模型?JavaFX 中的简单窗口应用程序