gpt4 book ai didi

swift - 将 View 置于 UITableViewCell 的中心?看起来位于 UI 层次结构的中心,但不在实际的应用程序中?

转载 作者:行者123 更新时间:2023-11-30 10:27:57 24 4
gpt4 key购买 nike

我正在尝试将自定义 UIView 置于 UITableViewCell 的中心。在我看来,这是相当基本的东西。我使用这段代码创建了 UIView,我在 stackoverflow 上找到了它:

class SkeletonView: UIView {

var startLocations : [NSNumber] = [-1.0,-0.5, 0.0]
var endLocations : [NSNumber] = [1.0,1.5, 2.0]

var gradientBackgroundColor : CGColor = UIColor(white: 0.85, alpha: 1.0).cgColor
var gradientMovingColor : CGColor = UIColor(white: 0.75, alpha: 1.0).cgColor

var movingAnimationDuration : CFTimeInterval = 0.8
var delayBetweenAnimationLoops : CFTimeInterval = 1.0


lazy var gradientLayer : CAGradientLayer = {

let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.bounds
gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
gradientLayer.colors = [
gradientBackgroundColor,
gradientMovingColor,
gradientBackgroundColor
]
gradientLayer.locations = self.startLocations
self.layer.addSublayer(gradientLayer)
return gradientLayer
}()


func startAnimating(){
let animation = CABasicAnimation(keyPath: "locations")
animation.fromValue = self.startLocations
animation.toValue = self.endLocations
animation.duration = self.movingAnimationDuration
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)


let animationGroup = CAAnimationGroup()
animationGroup.duration = self.movingAnimationDuration + self.delayBetweenAnimationLoops
animationGroup.animations = [animation]
animationGroup.repeatCount = .infinity
self.gradientLayer.add(animationGroup, forKey: animation.keyPath)
}

func stopAnimating() {
self.gradientLayer.removeAllAnimations()
}
}

然后我刚刚向我的单元格 xib 添加了一个 View ,水平居中,前缘 +15,顶部 +15 和底部 +15(优先级为 900,这样自动布局就不会因为高度为 320 而不是计算出来的而感到害怕)值为 320.1)。这样它就会在 xib 预览中居中。

enter image description here

运行应用程序时,它看起来像这样,您可以看到左侧有一个间隙,但右侧没有,因此它不居中:

uita

但是当我查看 UI 层次结构时,它是居中的?! enter image description here

最佳答案

enter image description here删除水平居中约束并将尾随约束添加到 15 将解决您的问题

我这边已经足够好了

关于swift - 将 View 置于 UITableViewCell 的中心?看起来位于 UI 层次结构的中心,但不在实际的应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59735376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com