问题
I'm iterating over UITabBar views like so:
for (UIView *view in [tabBar subviews]) {
if ([view isKindOfClass:[UIControl class]]) {
tabBarItemFrame = [tabBar convertRect:view.frame toView:self.view];
}
}
The order is initially:
<_UITabBarBackgroundView: 0x7a62e150; frame = (0 0; 768 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7a62fcd0>>,
<GSBadgeView: 0x7b1b6060; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7b1b61d0>>,
<UITabBarButton: 0x7d75ee80; frame = (71 1; 76 48); opaque = NO; layer = <CALayer: 0x7b0eaf80>>,
<UITabBarButton: 0x7d75ef90; frame = (181 1; 76 48); opaque = NO; layer = <CALayer: 0x7b0e7f40>>,
<UITabBarButton: 0x7d75f180; frame = (291 1; 76 48); opaque = NO; layer = <CALayer: 0x7b0c16e0>>,
<UITabBarButton: 0x7d75f6f0; frame = (401 1; 76 48); opaque = NO; layer = <CALayer: 0x7b0278a0>>,
<UITabBarButton: 0x7b026060; frame = (511 1; 76 48); opaque = NO; layer = <CALayer: 0x7b0244d0>>,
<UITabBarButton: 0x7b002960; frame = (621 1; 76 48); opaque = NO; layer = <CALayer: 0x7d7697c0>>,
<UIImageView: 0x7a62fdf0; frame = (0 -1; 768 1); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7a62bd50>>
After exiting the app and returning, however, the order changes to:
<_UITabBarBackgroundView: 0x7a62e150; frame = (0 0; 768 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7a62fcd0>>,
<GSBadgeView: 0x7b1b6060; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7b1b61d0>>,
<UITabBarButton: 0x7d75ef90; frame = (181 1; 76 48); opaque = NO; layer = <CALayer: 0x7b0e7f40>>,
<UITabBarButton: 0x7d75f180; frame = (291 1; 76 48); opaque = NO; layer = <CALayer: 0x7b0c16e0>>,
<UITabBarButton: 0x7d75f6f0; frame = (401 1; 76 48); opaque = NO; layer = <CALayer: 0x7b0278a0>>,
<UITabBarButton: 0x7b026060; frame = (511 1; 76 48); opaque = NO; layer = <CALayer: 0x7b0244d0>>,
<UITabBarButton: 0x7b002960; frame = (621 1; 76 48); opaque = NO; layer = <CALayer: 0x7d7697c0>>,
<UIImageView: 0x7a62fdf0; frame = (0 -1; 768 1); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7a62bd50>>,
<UITabBarButton: 0x7d75ee70; frame = (71 1; 76 48); opaque = NO; layer = <CALayer: 0x7b00ea80>>
Pretty much, the first tab bar button is at the end of the subviews
array. Visually the layout is correct and I'm not doing anything to manipulate the subviews.
Can anyone provide any insight here? Should I not rely on subviews to maintain a particular order?
Update
As @rmaddy mentions below, digging into subviews was unnecessary. I got the appropriate frame with the bit of code below:
NSInteger helpBarButtonIndex = [self.viewControllers indexOfObject:self.helpMenuVC];
UIView *helpBarButton = [[[self.tabBar items] objectAtIndex:helpBarButtonIndex] view];
tabBarItemFrame = [self.tabBar convertRect:helpBarButton.frame toView:self.view];
来源:https://stackoverflow.com/questions/32083932/uitabbar-subviews-change-order