0

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.

enter image description hereenter image description here

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.

enter image description here

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>>"
)
rose
  • 241
  • 5
  • 16
  • make sure the 'self.subviews' is returning the items in correct order !. btw you don'e need to subclass tabbar to make the bar like instagram. Check out tutorials – jpulikkottil Nov 12 '15 at 05:49
  • check the 'self.subviews' is returning items in correct order or not !. – jpulikkottil Nov 12 '15 at 05:57

2 Answers2

1

If 'self.subviews' returning items not in correct order, Set tag for each tab item. The tag values will be 0(for search), 1 (for contact), 3 and 4 (for settings) And inside the for loop change like:

if view is UIControl && !(view is UIButton) {
    view.frame = CGRectOffset(frame, view.tag * width, 0)
}
jpulikkottil
  • 666
  • 9
  • 24
1

I think your code here basically won't cause the problem, so I guess the cause comes from other code, so checkout other code or show us more code to help you. just like:
button creation code
button click action code
and so on

childrenOurFuture
  • 1,889
  • 2
  • 14
  • 23