约束未随设备方向更改而更新 [英] Constraints not updating with device orientation change

查看:80
本文介绍了约束未随设备方向更改而更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tableView单元格中有一个

  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 = .horizo​​ntal
colorSlider.addTarget(self,action:#selector(ConfigureTimestampTableViewController。 changedColor(_ :)),for:。valueChanged)

colorCell.contentView.addSubview(colorSlider)

colorSlide r.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 不会调整大小以响应他们的父 UIView 调整大小,无论是通过自动布局还是手动。



你只需要在你的 ColorSlider 中添加代码,当代码的大小/位置更新时 ColorSilider 更改。幸运的是, UIView 上有一个专门用于此的方法。

 覆盖func layoutSubviews(){
super.layoutSubviews()
gradientLayer.frame = bounds
}


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
}

这篇关于约束未随设备方向更改而更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆