我定制了一个UITabBar。在UITabBar中心添加一个按钮。 tabbar有4个tabbaritems和一个按钮。但是当我点击第三个tabbaritem时,它的位置变为第四个项目的位置。第三个tabbaritem和第四个项目交换他们的位置。
我设置了一个断点调试。当我在名为layoutSubviews()
的第三个babbaritem tabBar上录音时,我发现,但当我点击其他三个tabbaritems时,tabbar没有调用layoutSubviews()
。我不知道为什么。有人可以帮帮我吗?
我上传了代码。为每个UITabBarButton添加标记。然后使用标签乘法宽度。但它有一个新问题。第三项成为第一项。
这是固定的自定义UITarbar。
class YNNJTTabBar: UITabBar {
override func awakeFromNib() {
super.awakeFromNib()
self.addSubview(askQuestionButton)
addTagToUITarBarButton()
}
func addTagToUITarBarButton() {
var index = 0
for view in self.subviews {
if view is UIControl && !(view is UIButton) {
view.tag = index
index += (index == 1) ? 2 : 1
}
}
}
override func layoutSubviews() {
super.layoutSubviews()
let width = self.bounds.width / CGFloat(buttonCount)
let height = self.bounds.height
let frame = CGRect(x: 0, y: 0, width: width, height: height)
//set askQuestionButton's frame
let buttonFrame = CGRect(x: 0, y: 0, width: width - 12, height: height - 12)
askQuestionButton.frame = buttonFrame
askQuestionButton.center = CGPoint(x: self.bounds.width * 0.5, y: self.bounds.height * 0.5)
//set 4 item's frame
for view in self.subviews {
if view is UIControl && !(view is UIButton) {
print(view.tag)
view.frame = CGRectOffset(frame, width * CGFloat(view.tag), 0)
}
}
print(self.subviews as NSArray)
}
private let buttonCount = 5
let askQuestionButton: UIButton = {
let tempView = UIButton()
tempView.setBackgroundImage(UIImage(named: "askQuestionbtn"), forState: .Normal)
tempView.setTitle("问", forState: .Normal)
tempView.titleLabel?.font = UIFont.systemFontOfSize(22)
return tempView
}()
}
以下是推出应用时的打印信息
0
1
3
4
(
"<_UITabBarBackgroundView: 0x7fe47bc0d580; frame = (0 0; 320 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fe47bc02740>>",
"<UITabBarButton: 0x7fe47bccb060; frame = (0 0; 64 49); opaque = NO; layer = <CALayer: 0x7fe47bca51f0>>",
"<UITabBarButton: 0x7fe47bccc9e0; frame = (64 0; 64 49); opaque = NO; tag = 1; layer = <CALayer: 0x7fe47bcccf80>>",
"<UITabBarButton: 0x7fe47bccea80; frame = (192 0; 64 49); opaque = NO; tag = 3; layer = <CALayer: 0x7fe47bccf020>>",
"<UITabBarButton: 0x7fe47bcd0ad0; frame = (256 0; 64 49); opaque = NO; tag = 4; layer = <CALayer: 0x7fe47bcd1050>>",
"<UIButton: 0x7fe47bcca690; frame = (134 6; 52 37); opaque = NO; layer = <CALayer: 0x7fe47bca3980>>",
"<UIImageView: 0x7fe47bc22b80; frame = (0 -0.5; 320 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fe47bc07b80>>"
)
当我点击第三项
时0
1
4
0
(
"<_UITabBarBackgroundView: 0x7fe47bc0d580; frame = (0 0; 320 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fe47bc02740>>",
"<UITabBarButton: 0x7fe47bccb060; frame = (0 0; 64 49); opaque = NO; layer = <CALayer: 0x7fe47bca51f0>>",
"<UITabBarButton: 0x7fe47bccc9e0; frame = (64 0; 64 49); opaque = NO; tag = 1; layer = <CALayer: 0x7fe47bcccf80>>",
"<UITabBarButton: 0x7fe47bcd0ad0; frame = (256 0; 64 49); opaque = NO; tag = 4; layer = <CALayer: 0x7fe47bcd1050>>",
"<UIButton: 0x7fe47bcca690; frame = (134 6; 52 37); opaque = NO; layer = <CALayer: 0x7fe47bca3980>>",
"<UIImageView: 0x7fe47bc22b80; frame = (0 -0.5; 320 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fe47bc07b80>>",
"<UITabBarButton: 0x7fe47bcd0150; frame = (0 0; 64 49); opaque = NO; layer = <CALayer: 0x7fe47bcb8f10>>"
)
答案 0 :(得分:1)
如果'self.subviews'返回的项目顺序不正确, 为每个标签项设置标记。 标签值将为0(用于搜索),1(用于联系),3和4(用于设置) 在for循环内部改变如:
if view is UIControl && !(view is UIButton) {
view.frame = CGRectOffset(frame, view.tag * width, 0)
}
答案 1 :(得分:1)
我认为你的代码基本上不会导致问题,所以我猜原因来自其他代码,所以检查其他代码或向我们展示更多代码来帮助你。就像:
按钮创建代码
按钮单击操作代码
等等