gpt4 book ai didi

ios - 在 Customize UIView 上添加 subview 效果不如预期

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

我将 UIView 的类子类化为:AURTabView。然后我将 3 个 View (白色背景)拖到 Storyboard 中,以每个 View 的 1/3 屏幕宽度自动布局它们。然后,我在 init 方法上添加了一个 UIButton,目前效果很好,如下所示:enter image description here

然后我想在它们上面添加另外两个 UIView。奇怪的是第一个和第三个 AUTRabView 按预期工作,但中间的没有。

enter image description here

这真的很奇怪。我检查了如下 UIView 层次结构:enter image description here

有什么要点吗?

代码如下:

class AURTabView: UIView {

let tabButton = UIButton()
let smallCircle = UIView()
let largeCircle = UIView()

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

self.addSubview(tabButton)
self.addSubview(smallCircle)
self.addSubview(largeCircle)
}

override func layoutSubviews() {
super.layoutSubviews()

let height = self.frame.height
tabButton.frame = CGRect(x: (self.frame.width-height)/2, y: 0, width: height, height: height)
tabButton.backgroundColor = UIColor.greenColor()

smallCircle.frame = CGRect(x: CGRectGetMidX(self.frame)-2.5, y: height-10-8, width: 5, height: 5)
smallCircle.backgroundColor = UIColor.redColor()
largeCircle.frame = CGRect(x: CGRectGetMidX(self.frame)-5, y: height-8, width: 10, height: 10)
largeCircle.backgroundColor = UIColor.redColor()
print(smallCircle)
print(largeCircle)
}

override func drawRect(rect: CGRect) {
tabButton.layer.cornerRadius = tabButton.frame.width/2
}
}

最佳答案

使用这个

import UIKit

AURTabView 类:UIView {

let tabButton   = UIButton()
let smallCircle = UIView()
let largeCircle = UIView()

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

self.addSubview(tabButton)
self.addSubview(smallCircle)
self.addSubview(largeCircle)
}

override func layoutSubviews() {
super.layoutSubviews()

let height = self.frame.height
tabButton.frame = CGRect(x: (self.frame.width-height)/2, y: 0, width: height, height: height)
tabButton.backgroundColor = UIColor.greenColor()

smallCircle.frame = CGRect(x: self.frame.width/2 - 2.5, y: height-10-8, width: 5, height: 5)
smallCircle.backgroundColor = UIColor.blackColor()
largeCircle.frame = CGRect(x: self.frame.width/2 - 5, y: height-8, width: 10, height: 10)
largeCircle.backgroundColor = UIColor.redColor()
print(smallCircle)
print(largeCircle)
}

override func drawRect(rect: CGRect) {
tabButton.layer.cornerRadius = tabButton.frame.width/2
}

关于ios - 在 Customize UIView 上添加 subview 效果不如预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140299/

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