在 iPad 应用程序中更改 UITabbarItems 的宽度和它们之间的边距

发布于 2024-12-02 04:11:45 字数 153 浏览 2 评论 0原文

是否可以更改 UITabbar 中 UITabbarItem 的宽度以及 iPad 应用程序的每个 UITabbarItem 之间的边距?

问题是标准边距对于我的布局来说太大 - 客户希望选项卡更紧凑:/

或者我应该混乱 UIToolbar 来实现这个目标吗?

Is it possible to change the width of UITabbarItem in UITabbar and margins between each UITabbarItem for iPad Application?

The problem is that standard margins are too large for my layout - client want tabs to be more compact :/

Or should I mess with UIToolbar to achieve this goal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不美如何 2024-12-09 04:11:45

对于选项卡栏项目宽度:

[[UITabBar appearance] setItemWidth:self.window.frame.size.width/NUMBER_OF_ITEMS];

对于选项卡栏框架:

[self.tabBar setFrame:rectFrame];

For Tabbar item width :

[[UITabBar appearance] setItemWidth:self.window.frame.size.width/NUMBER_OF_ITEMS];

For tabbar frame :

[self.tabBar setFrame:rectFrame];
盗琴音 2024-12-09 04:11:45

我已经通过这种方式解决了同样的问题:
我的目标是使用设计师为我发送的图像来自定义选项卡栏。

我已经附加了项目的.png文件,初始化了UIImage *类型的相应变量,并使用UITabBarItem函数setFinishedSelectedImage:withFinishedUnselectedImage:设置UITabbarItem的活动/非活动状态的图像:

UIImage * left_active, *left_inactive, *center_active, *center_inactive, *right_active, *right_inactive;
left_active = [UIImage imageNamed:@"left_active_img"];
...
[self.leftTabBarItem setFinishedSelectedImage:left_active withFinishedUnselectedImage:left_inactive];
[self.centerTabBarItem setFinishedSelectedImage:center_active withFinishedUnselectedImage:center_inactive];
[self.rightTabBarItem setFinishedSelectedImage:right_active withFinishedUnselectedImage:right_inactive];

但是自定义的tabbarItems比设计器的图像小并被一个接一个地分配在屏幕中央:

2)为了解决这个问题,我添加了额外的 UITABBARITEMS - 在左侧在初始的右上角

3) 创建了 UITabBarItems 的相应出口:

.h 文件:

@property (nonatomic, retain) IBOutlet UITabBar * tabBar;
// THE INTIAL items
@property (nonatomic, retain) IBOutlet UITabBarItem * leftTabBarItem;
@property (nonatomic, retain) IBOutlet UITabBarItem * centerTabBarItem;
@property (nonatomic, retain) IBOutlet UITabBarItem * rightTabBarItem;

// THE ADDITIONAL items
@property (nonatomic, retain) IBOutlet UITabBarItem * left_1;
@property (nonatomic, retain) IBOutlet UITabBarItem * left_2;
@property (nonatomic, retain) IBOutlet UITabBarItem * center_1;
@property (nonatomic, retain) IBOutlet UITabBarItem * center_2;
@property (nonatomic, retain) IBOutlet UITabBarItem * right_1;
@property (nonatomic, retain) IBOutlet UITabBarItem * right_2;

然后按照下面列出的顺序将出口附加到 UITabBarItems:

  • left_1
  • leftTabBarItem
  • left_2
  • center_1
  • centerTabBarItem
  • center_2
  • right_1
  • rightTabBarItem
  • right_2

并已更正委托类中的 UITabbarDelegate 方法仅切换甜菜可见项目
.m 文件:

#pragma mark - UITabbarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    if (item == self.leftTabBarItem) 
    {
        NSLog(@"0"); // first visible item selected

    } else if (item == self.centerTabBarItem)
    {
        NSLog(@"1"); // second visible item selected   

    } else if (item == self.rightTabBarItem)
    {
        NSLog(@"2"); // third visible item selected
    } else if (item == self.left_1){
        [self.tabBar setSelectedItem: self.leftTabBarItem];
    } else if (item == self.left_2){
        [self.tabBar setSelectedItem: self.leftTabBarItem];
    } else if (item == self.center_1){
        [self.tabBar  setSelectedItem: self.centerTabBarItem];
    }else if (item == self.center_2){
        [self.tabBar setSelectedItem: self.centerTabBarItem];
    }else if (item == self.right_1){
        [self.tabBar setSelectedItem: self.rightTabBarItem];
    } else if (item == self.right_2){
        [self.tabBar setSelectedItem: self.rightTabBarItem];
    }
}

现在一切看起来都正常并且工作正常。

您可以使用相同的步骤通过添加其他项目和更正委托方法来自定义 UITabBarItems 之间的大小和间距。

I have solved just the same problem it this way:
my goal was to customize tab bar by using images, that were sent by the designer for me.

I have attached the .png files for the project, initialized the correspondent variables of type UIImage* and used UITabBarItem function setFinishedSelectedImage: withFinishedUnselectedImage: to set the images for active / inactive state of UITabbarItem:

UIImage * left_active, *left_inactive, *center_active, *center_inactive, *right_active, *right_inactive;
left_active = [UIImage imageNamed:@"left_active_img"];
...
[self.leftTabBarItem setFinishedSelectedImage:left_active withFinishedUnselectedImage:left_inactive];
[self.centerTabBarItem setFinishedSelectedImage:center_active withFinishedUnselectedImage:center_inactive];
[self.rightTabBarItem setFinishedSelectedImage:right_active withFinishedUnselectedImage:right_inactive];

But the customized tabbarItems were smaller, than the designer's images and were allocated at the center of the screen one upon other:

2) To fix this I have ADDED ADDITIONAL UITABBARITEMS - at the left and at the right corner of the initial ones

3) the were created the correspondent outlets for the UITabBarItems:

.h-file:

@property (nonatomic, retain) IBOutlet UITabBar * tabBar;
// THE INTIAL items
@property (nonatomic, retain) IBOutlet UITabBarItem * leftTabBarItem;
@property (nonatomic, retain) IBOutlet UITabBarItem * centerTabBarItem;
@property (nonatomic, retain) IBOutlet UITabBarItem * rightTabBarItem;

// THE ADDITIONAL items
@property (nonatomic, retain) IBOutlet UITabBarItem * left_1;
@property (nonatomic, retain) IBOutlet UITabBarItem * left_2;
@property (nonatomic, retain) IBOutlet UITabBarItem * center_1;
@property (nonatomic, retain) IBOutlet UITabBarItem * center_2;
@property (nonatomic, retain) IBOutlet UITabBarItem * right_1;
@property (nonatomic, retain) IBOutlet UITabBarItem * right_2;

then attached the Outlets to the UITabBarItems in the order listed below:

  • left_1
  • leftTabBarItem
  • left_2
  • center_1
  • centerTabBarItem
  • center_2
  • right_1
  • rightTabBarItem
  • right_2

and CORRECTED THE UITabbarDelegate METHOD in the delegating class to switch ONLY beet wen the visible items
.m-file:

#pragma mark - UITabbarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    if (item == self.leftTabBarItem) 
    {
        NSLog(@"0"); // first visible item selected

    } else if (item == self.centerTabBarItem)
    {
        NSLog(@"1"); // second visible item selected   

    } else if (item == self.rightTabBarItem)
    {
        NSLog(@"2"); // third visible item selected
    } else if (item == self.left_1){
        [self.tabBar setSelectedItem: self.leftTabBarItem];
    } else if (item == self.left_2){
        [self.tabBar setSelectedItem: self.leftTabBarItem];
    } else if (item == self.center_1){
        [self.tabBar  setSelectedItem: self.centerTabBarItem];
    }else if (item == self.center_2){
        [self.tabBar setSelectedItem: self.centerTabBarItem];
    }else if (item == self.right_1){
        [self.tabBar setSelectedItem: self.rightTabBarItem];
    } else if (item == self.right_2){
        [self.tabBar setSelectedItem: self.rightTabBarItem];
    }
}

Now everything looks and works properly.

You can use the same steps to customize the size and interspaces between UITabBarItems by adding additional items and correcting delegate methods.

甜宝宝 2024-12-09 04:11:45

我认为这是不可能的。您可以创建自定义选项卡栏。另外,弄乱默认的 UItabbar 可能会导致应用程序审批过程中被拒绝。

I don't think it is possible. You can create a custom tab bar. Also messing around with default UItabbar might cause rejection during App approval process.

夜还是长夜 2024-12-09 04:11:45

您可以通过子类化 UITabBar 并重写其 layoutSubviews 方法来更改选项卡栏项目的间距。您将在 self.subViews 数组中找到所有选项卡栏按钮。它们的类是非公共的 UITabBarButton,但它们继承自 UIControl,因此您可以识别所有选项卡栏按钮,检查它们是否属于 UIControl 类。然后您所需要做的就是更改选项卡栏按钮的框架。

You can change the spacing of the tab bar items by subclassing UITabBar and overriding its layoutSubviews method. You will find all tab bar buttons in the self.subViews array. Their class is the non-public UITabBarButton but they inherit from UIControl so you can identify all tab bar buttons checking if they are kind of UIControl class. Then all you need is to change the frame of the tab bar buttons.

淡紫姑娘! 2024-12-09 04:11:45

您可以使用 setSelectionIndicatorImage 使选项卡更加紧凑。

SelectionIndicatorImage 宽度匹配

iPad 中的 UITabBarItem 宽度将与AppDelegate.m 中的

[[UITabBar appearance] setItemWidth:self.window.frame.size.width/NUMBER_OF_YOUR_TAB];
[[UITabBar appearance] setItemSpacing:0];
[[UITabBar appearance] setSelectionIndicatorImage:[self imageFromColor:[UIColor clearColor] forSize:CGSizeMake(self.window.frame.size.width/NUMBER_OF_YOUR_TAB, 10) withCornerRadius:0]];

,您可以使用下面的代码以编程方式创建图像

- (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
    UIGraphicsBeginImageContext(size);
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];

    // Draw your image
    [image drawInRect:rect];

    image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

You can use setSelectionIndicatorImage to make your tab more compact.

The UITabBarItem width in iPad will match the selectionIndicatorImage's width

in AppDelegate.m

[[UITabBar appearance] setItemWidth:self.window.frame.size.width/NUMBER_OF_YOUR_TAB];
[[UITabBar appearance] setItemSpacing:0];
[[UITabBar appearance] setSelectionIndicatorImage:[self imageFromColor:[UIColor clearColor] forSize:CGSizeMake(self.window.frame.size.width/NUMBER_OF_YOUR_TAB, 10) withCornerRadius:0]];

you can use below code to create a image programmatically

- (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
    UIGraphicsBeginImageContext(size);
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];

    // Draw your image
    [image drawInRect:rect];

    image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}
无需解释 2024-12-09 04:11:45

不需要子类化。

tabBarController.tabBar.itemPositioning = .centered
tabBarController.tabBar.itemWidth = 40
tabBarController.tabBar.itemSpacing = 38

No subclassing is needed.

tabBarController.tabBar.itemPositioning = .centered
tabBarController.tabBar.itemWidth = 40
tabBarController.tabBar.itemSpacing = 38
温柔嚣张 2024-12-09 04:11:45

您无法更改 UITabBarItem 的宽度,即使在子类化 UITabBarItem 之后也无法执行此操作。苹果公司对开发者实施应用程序的方式有一定的限制。

为此,您需要使用 UIToolBar,我最好的选择是您转到 github,因为那里有很多使用可滚动 UIToolBar 的示例应用程序底部而不是 UITabBar

You cannot change the width of the the UITabBarItem, and you cannot do this even after subclassing the UITabBarItem. Apple has certain restrictions in the way they want the developers to implement the applications.

You will need to use a UIToolBar for this, and my best bet is for you to go to github and as there a lot of sample applications which use a scrollable UIToolBar at the bottom instead of the UITabBar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文