gpt4 book ai didi

ios - 自定义 UIView 的 subview 有错误的 x 框架位置

转载 作者:行者123 更新时间:2023-11-29 11:49:11 25 4
gpt4 key购买 nike

我有一个像这样的自定义 UIView:

import UIKit

class MainLogoAnimationView: UIView {

@IBOutlet var customView: UIView!
var turquoiseCircle: AnimationCircleView!
var redCircle: AnimationCircleView!
var blueCircle: AnimationCircleView!


required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

func setup() {
_ = Bundle.main.loadNibNamed("MainLogoAnimationView", owner: self, options: nil)?[0] as! UIView
self.addSubview(customView)
customView.frame = self.bounds
setupAnimation()
}

let initialWidthScaleFactor: CGFloat = 0.06

func setupAnimation() {
let circleWidth = frame.size.width*initialWidthScaleFactor
let yPos = frame.size.height/2 - circleWidth/2
turquoiseCircle = AnimationCircleView(frame: CGRect(x: 0, y: yPos, width: circleWidth, height: circleWidth))
turquoiseCircle.circleColor = UIColor(red: 137/255.0, green: 203/255.0, blue: 225/255.0, alpha: 1.0).cgColor
turquoiseCircle.backgroundColor = UIColor.lightGray
addSubview(turquoiseCircle)
redCircle = AnimationCircleView(frame: CGRect(x: 100, y: yPos, width: circleWidth, height: circleWidth))
addSubview(redCircle)
blueCircle = AnimationCircleView(frame: CGRect(x: 200, y: yPos, width: circleWidth, height: circleWidth))
blueCircle.circleColor = UIColor(red: 93/255.0, green: 165/255.0, blue: 213/255.0, alpha: 1.0).cgColor
addSubview(blueCircle)
}
}

我在界面生成器中创建了一个浅蓝色的 UIView,并将上面的 View MainLogoAnimationView 设置为它的子类。如果我运行该应用程序,我会得到这个:

enter image description here

似乎对于 turquoiseCircle 框架没有像我设置的那样放置在 x = 0 处。不确定我在这里做错了什么。是因为放在 MainLogoAnimationView 完全初始化之前所以放错了吗?我已经尝试在 layoutSubviews() 中设置圆圈的框架,这也没有任何区别。我似乎经常遇到这个问题,因为观点放错了地方,我认为我在这里遗漏了一些基本的东西。我做错了什么?

更新:

import UIKit

class MainLogoAnimationView: UIView {

@IBOutlet var customView: UIView!
var turquoiseCircle: AnimationCircleView!
var redCircle: AnimationCircleView!
var blueCircle: AnimationCircleView!


required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

func setup() {
_ = Bundle.main.loadNibNamed("MainLogoAnimationView", owner: self, options: nil)?[0] as! UIView
self.addSubview(customView)
customView.frame = self.bounds
setupAnimation()
}

let initialWidthScaleFactor: CGFloat = 0.2


func setupAnimation() {
blueCircle = AnimationCircleView()
blueCircle.translatesAutoresizingMaskIntoConstraints = false
blueCircle.backgroundColor = UIColor.lightGray
blueCircle.circleColor = UIColor.blue.cgColor
addSubview(blueCircle)

redCircle = AnimationCircleView()
redCircle.translatesAutoresizingMaskIntoConstraints = false
redCircle.backgroundColor = UIColor.lightGray
addSubview(redCircle)
}

override func layoutSubviews() {
super.layoutSubviews()
let circleWidth = bounds.size.width*initialWidthScaleFactor
let yPos = frame.size.height/2 - circleWidth/2
blueCircle.frame = CGRect(x: 0, y: yPos, width: circleWidth, height: circleWidth)
redCircle.frame = CGRect(x: frame.size.width/2 - circleWidth/2, y: yPos, width: circleWidth, height: circleWidth)
}
}

和:

导入 UIKit

class AnimationCircleView: UIView {

var circleColor = UIColor(red: 226/255.0, green: 131/255.0, blue: 125/255.0, alpha: 1.0).cgColor {
didSet {
shapeLayer.fillColor = circleColor
}
}

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

let shapeLayer = CAShapeLayer()

func setup() {
let circlePath = UIBezierPath(ovalIn: self.bounds)
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = circleColor
shapeLayer.frame = self.bounds
layer.addSublayer(shapeLayer)
}

}

enter image description here

最佳答案

肯定是init中的设置会提供self.frame/self.bounds在 View 层次结构中布局时,无论 View 的初始化值是什么,而不是正确的最终值。处理此问题的最佳方法可能就像您尝试过的那样,在 init 中添加 View ,并使用属性保留对它们的引用(您已经在这样做),然后在 -layoutSubviews() 中设置框架。 .

它可能在 -layoutSubviews() 中不起作用的原因对于创建后的每个 View ,您需要调用 .translatesAutoresizingMaskIntoConstraints = false .否则,对于以编程方式创建的 View ,系统将在添加为 subview 时为值创建约束,从而覆盖框架的任何设置。尝试设置它,看看它是如何进行的。

编辑:为了调整 CAShapeLayer 的大小,而不是添加为子层(这需要手动调整大小),因为 AnimationCirleView View 是专门的 View 可以由 CAShapeLayer 支持。在 Overriding default layer type in UIView in swift 查看答案

关于ios - 自定义 UIView 的 subview 有错误的 x 框架位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42006395/

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