简体   繁体   English

iOS在UITabBar中更改UITabBarButton的框架

[英]iOS Change frame of UITabBarButton in UITabBar

In my app I have a UITabBarController in which I give custom selected/unselected images in my AppDelegate as follows: 在我的应用程序中,我有一个UITabBarController ,我在AppDelegate中给AppDelegate定义选定/未选择的图像,如下所示:

UIImage *selectedImage = [UIImage imageNamed:@"home-tab-selected"];
UIImage *unselectedImage = [UIImage imageNamed:@"home-tab"];
UITabBar *tabBar = tabController.tabBar;
UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
[item1 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];

The images I have are 100x100, much larger than the normal tab bar items. 我的图像是100x100,比普通的标签栏项目大得多。 All works well, and my images are placed nicely and look great. 一切都运作良好,我的图像放置得很好,看起来很棒。

The issue I am having is that the frame of the underlaying UITabBarButton remains 76x48, leaving only a small portion of my tabs 'touchable'. 我遇到的问题是底层UITabBarButton的框架仍然是76x48,只留下我的标签的一小部分'可触摸'。 (Image below with a border around the frame) (下图中框架周围有边框)

框架小于标签图像

To attempt to fix this, in my subcalssed UITabBarController viewDidLoad , I go through each UITabBarButton , and set the frame as follows: 为了解决这个问题,在我的子代UITabBarController viewDidLoad ,我浏览了每个UITabBarButton ,并按如下方式设置框架:

for (UIView* subView in self.tabBar.subviews)
{
    if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")])
    {

        [subView setFrame:CGRectMake(subView.frame.origin.x, subView.frame.origin.y, 100, 100)];


    }
}

After I do that, I log the frames and they do change-- BUT the frame never changes in my tab bar-- I still only see and can touch in the smaller red box. 在我这样做之后,我记录了帧并且它们确实发生了变化 - 但是我的标签栏中的框架永远不会改变 - 我仍然只能看到并且可以触摸较小的红色框。

Is this because Apple does not allow you to change these frames or am I doing something wrong here? 这是因为Apple不允许您更改这些帧或我在这里做错了吗?

Any help apperciated! 任何帮助apperciated! Thanks! 谢谢!

EDIT: 编辑:

In the end I just ended up making my tab images smaller. 最后,我最终使我的标签图像变小了。 I found a height of 70px still picks up most touches. 我发现70px的高度仍然可以吸引大多数人。

The layout of your UITabBarController 's view might change after loading: Additional UITabBarItem s could be added, of the user could switch to landscape orientation. 加载后, UITabBarController视图的布局可能会发生变化:可以添加其他UITabBarItem ,用户可以切换到横向。

Before iOS 6, there wasn't a way to configure complex layouts. 在iOS 6之前,没有办法配置复杂的布局。 Since UITabBar is a very old class, it's most likely doing its work in the layoutSubviews method. 由于UITabBar是一个非常古老的类,因此很可能在layoutSubviews方法中完成它的工作。

You could subclass UITabBar and override said method, but if you go that far, you might want to think about creating your own tab bar, which in the end is just a UIView with a few UIButton s. 您可以UITabBar并覆盖所述方法,但是如果你走得那么远,你可能想要创建自己的标签栏,最后它只是一个带有一些UIButtonUIView

Had a similiar problem with the underlying UITabBarButtons. 与底层的UITabBarButtons有类似的问题。 I solved it by setting the UITabBar's "Item positioning" attribute to "Fill". 我通过将UITabBar的“项目定位”属性设置为“填充”来解决它。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM