简体   繁体   English

如何删除UITabBarItem selectionImage填充?

[英]How do I remove UITabBarItem selectionImage padding?

I am setting the SelectionIndicatorImage as a stretchable image (to accommodate for various device widths) via UITabBar appearance: 我通过UITabBar外观将SelectionIndicatorImage设置为可伸缩图像(以适应各种设备宽度):

UIImage *selectedImage = [[UIImage imageNamed:@"SelectedTab"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
[[UITabBar appearance] setSelectionIndicatorImage:selectedImage];

As a result I get a 2pt padding on the screen edges. 结果我在屏幕边缘上得到了一个2pt填充。 I am basically trying to use selection indicator as a background for currently selected UITabBarItem. 我基本上试图使用选择指标作为当前所选UITabBarItem的背景。

http://i.stack.imgur.com/qFHEk.png http://i.stack.imgur.com/qFHEk.png

What would be an easy way to solve this? 什么是解决这个问题的简单方法?

If you want to use UITabBarController in your app with selection and unselection feature then you should this code 如果你想在你的应用程序中使用具有选择和取消选择功能的UITabBarController,那么你应该使用这个代码

[[self.tabBarController tabBar] setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg_tabBar" ofType:@"png"]]];
[[self.tabBarController tabBar] setSelectionIndicatorImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg_tabItem_selected" ofType:@"png"]]];

NSArray *arrTabItems = [[self.tabBarController tabBar] items];
UITabBarItem *tabBarItem = [arrTabItems objectAtIndex:0];
[tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"img_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"img_unselected.png"]];
[tabBarItem setImageInsets:UIEdgeInsetsMake(5, 5, 0, 5)];

I've tried something similar. 我尝试过类似的东西。 Ended up this way: 结束这种方式:

Subclass UITabBarController and add property: 子类UITabBarController并添加属性:

@property(nonatomic, readonly) UIImageView *imageView;

Setup your image as you want: 根据需要设置图像:

- (void)viewDidLoad {
    [super viewDidLoad];

    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbaritem"]];
    [self.tabBar addSubview:self.imageView];
}

Place your subview in correct position: 将您的子视图放在正确的位置:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    // get all UITabBarButton views
    NSMutableArray *tabViews = [NSMutableArray new];
    for (UIView *view in self.tabBar.subviews) {
        if ([view isKindOfClass:[UIControl class]]) {
            [tabViews addObject:[NSValue valueWithCGRect:view.frame]];
        }
    }

    // sort them from left to right
    NSArray *sortedArray = [tabViews sortedArrayUsingComparator:^NSComparisonResult(NSValue *firstValue, NSValue *secondValue) {
        CGRect firstRect = [firstValue CGRectValue];
        CGRect secondRect = [secondValue CGRectValue];

        return CGRectGetMinX(firstRect) > CGRectGetMinX(secondRect);
    }];

    // place your imageView on selected index button frame
    CGRect frame = self.tabBar.bounds;
    CGSize imageSize = CGSizeMake(CGRectGetWidth(frame) / self.tabBar.items.count, self.imageView.image.size.height);
    CGRect selectedRect = sortedArray.count > self.selectedIndex ? [sortedArray[self.selectedIndex] CGRectValue] : CGRectZero;
    [self.imageView setFrame:CGRectIntegral(CGRectMake(CGRectGetMinX(selectedRect), CGRectGetMaxY(frame) - imageSize.height,
                                                       CGRectGetWidth(selectedRect), imageSize.height))];
}

Then you only need to refresh layout on each tab change: 然后,您只需要在每个选项卡更改时刷新布局:

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    [super setSelectedIndex:selectedIndex];

    [self.view setNeedsLayout];
}

- (void)setSelectedViewController:(UIViewController *)selectedViewController {
    [super setSelectedViewController:selectedViewController];

    [self.view setNeedsLayout];
}

Old answer how to remove padding (this is what I firstly understood as your question, I'm leaving it as a reference for other people). 旧的回答如何删除填充(这是我首先理解为你的问题,我将其作为其他人的参考)。

In iOS 7+ UITabBar has following property: 在iOS 7+中, UITabBar具有以下属性:

/*
 Set the itemSpacing to a positive value to be used between tab bar items
 when they are positioned as a centered group.
 Default of 0 or values less than 0 will be interpreted as a system-defined spacing.
 */
@property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

This should enable you to get rid of padding between items. 这应该可以让你摆脱项之间的填充。

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

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