简体   繁体   English

对于iphone 6和iPhone 6+,setSelectionIndicatorImage的大小不正确

[英]setSelectionIndicatorImage is of wrong size for iphone 6 and iPhone 6+

I am using following method to set selection indicator for selected tab bar item. 我正在使用以下方法为所选标签栏项目设置选择指示器。 It works well for iPhone 4/4s/5/5s but not in iphone 6/6+. 它适用于iPhone 4 / 4s / 5 / 5s但不适用于iphone 6/6 +。

[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"activeshape.png"] ];

Any suggestion 任何建议

EDIT: IT seems that after all this solution should work, I had some issues with cache 编辑:IT似乎毕竟这个解决方案应该工作,我有一些缓存问题

UIImage *selTab = [[UIImage imageNamed:@"tabHighlight"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
CGSize tabSize = CGSizeMake(CGRectGetWidth(self.view.frame)/5, 49);
UIGraphicsBeginImageContext(tabSize);
[selTab drawInRect:CGRectMake(0, 0, tabSize.width, tabSize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//
[self.tabBar setSelectionIndicatorImage:reSizeImage];

tabHiglight is a png of 82x49, I've tested with other sizes but this seems to fit best. tabHiglight是一个tabHiglight的png,我已经测试过其他尺寸,但这似乎最合适。 I divide the width of the frame by the number of items I have in the tabBar - in my case 5, then I create a new UIImage of the "right" size and set it as the selectionIndicatorImage . 我将框架的width除以tabBar中的项目数 - 在我的情况下为5,然后我创建一个“正确”大小的新UIImage并将其设置为selectionIndicatorImage

Here is my auto-adjusting UITabBarController subclass. 这是我自动调整的UITabBarController子类。 Just provide an image and it will adjust to all known iPhones and iPads. 只需提供一个图像,它将适应所有已知的iPhone和iPad。 It will also update the size whenever the trait collection changes so it supports all the new iPad features and rotation. 每当特征集合发生变化时,它也会更新大小,因此它支持所有新的iPad功能和旋转。

import UIKit

class TabBarController: UITabBarController {
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        updateSelectionIndicatorImage()
    }

    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)

        updateSelectionIndicatorImage()
    }

    func updateSelectionIndicatorImage() {
        let width = CGRectGetWidth(tabBar.bounds) > 420 ? 420 : CGRectGetWidth(tabBar.bounds)
        var selectionImage = UIImage(named: "TabSelectionIndicator")
        let tabSize = CGSizeMake(width/5, 49)

        UIGraphicsBeginImageContext(tabSize)
        selectionImage?.drawInRect(CGRectMake(0, 0, tabSize.width, tabSize.height))
        selectionImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        tabBar.selectionIndicatorImage = selectionImage
    }
}

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

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