Constraints not updating with device orientation change(约束不随设备方向更改而更新)
问题描述
我在 tableView 单元格中有一个
func configureColorSlider() {让 colorSlider = ColorSlider()让 xCell = colorCell.contentView.bounds.width让 yCell = colorCell.contentView.bounds.heightcolorSlider.frame = CGRect(x: xCell/4, y: yCell/4, 宽度: 200, 高度: 24)colorSlider.orientation = .horizontalcolorSlider.addTarget(self, action: #selector(ConfigureTimestampTableViewController.changedColor(_:)), for: .valueChanged)colorCell.contentView.addSubview(colorSlider)colorSlider.translatesAutoresizingMaskIntoConstraints = falseNSLayoutConstraint.activate([colorSlider.leadingAnchor.constraint(equalTo: colorLabel.trailingAnchor, 常量: 8),colorSlider.trailingAnchor.constraint(equalTo: colorCell.contentView.trailingAnchor, 常量: -8),colorSlider.topAnchor.constraint(equalTo: colorCell.contentView.topAnchor, 常量: 8),colorSlider.bottomAnchor.constraint(equalTo:colorCell.contentView.bottomAnchor,常量:-8)])}
CALayers
不会响应其父 UIView
被调整大小而调整大小,无论是通过自动布局还是手动.
您只需向 ColorSlider
添加代码,当 ColorSilider
的大小/位置发生变化时,该代码会更新图层.幸运的是,UIView
上有一个专门用于此的方法.
覆盖 func layoutSubviews() {super.layoutSubviews()gradientLayer.frame = 边界}
I have a color slider in a tableView cell that extends from the label to the trailingAnchor of the cell. The issue I am having is when the device orientation changes the color slider does not update its constraints and no longer extends the full length of the cell. Below is my code to set up the constraints on the color slider. Below are images to show the issue I am having. If my phone is already in landscape orientation when I present this scene the color slider extends the full length of the cell as desired. However, if I switch to landscape when already viewing this scene the slider appears as below. Here is the full code if that helps.
func configureColorSlider() {
let colorSlider = ColorSlider()
let xCell = colorCell.contentView.bounds.width
let yCell = colorCell.contentView.bounds.height
colorSlider.frame = CGRect(x: xCell / 4, y: yCell / 4, width: 200, height: 24)
colorSlider.orientation = .horizontal
colorSlider.addTarget(self, action: #selector(ConfigureTimestampTableViewController.changedColor(_:)), for: .valueChanged)
colorCell.contentView.addSubview(colorSlider)
colorSlider.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([colorSlider.leadingAnchor.constraint(equalTo: colorLabel.trailingAnchor, constant: 8),
colorSlider.trailingAnchor.constraint(equalTo: colorCell.contentView.trailingAnchor, constant: -8),
colorSlider.topAnchor.constraint(equalTo: colorCell.contentView.topAnchor, constant: 8),
colorSlider.bottomAnchor.constraint(equalTo: colorCell.contentView.bottomAnchor, constant: -8) ])
}
CALayers
will not be resized in response to their parent UIView
being resized, whether by auto layout or manually.
You just need to add code to your ColorSlider
that updates the layer when the size/position of the ColorSilider
changes. Luckily, there is a method on UIView
that is specifically for this.
override func layoutSubviews() {
super.layoutSubviews()
gradientLayer.frame = bounds
}
这篇关于约束不随设备方向更改而更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:约束不随设备方向更改而更新
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01