I customed a UITabBar. Add a button to center of UITabBar. tabbar have 4 tabbaritems and a button. But when I tap on the third tabbaritem, its position changed to the fourth item's position. Third tabbaritem and fourth item exchange their position.
I set a breakpoint debugging. I found when I taped on third babbaritem tabBar called layoutSubviews()
, but when I tap on the other three tabbaritems, tabbar didn't call layoutSubviews()
. I don't konw why. Anybody can help me?
I upload the code. add tag to every UITabBarButton. then use tag multiply width. But it has a new problem. the third item become the first item.
Here is the fixed custom 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
}()
}
Here is print information, when app launched
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>>"
)
when I tap on third item
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>>"
)