[英]setSelectionIndicatorImage is of wrong size for iphone 6 and iPhone 6+
我正在使用以下方法為所選標簽欄項目設置選擇指示器。 它適用於iPhone 4 / 4s / 5 / 5s但不適用於iphone 6/6 +。
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"activeshape.png"] ];
任何建議
編輯: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
是一個tabHiglight
的png,我已經測試過其他尺寸,但這似乎最合適。 我將框架的width
除以tabBar中的項目數 - 在我的情況下為5,然后我創建一個“正確”大小的新UIImage並將其設置為selectionIndicatorImage
。
這是我自動調整的UITabBarController
子類。 只需提供一個圖像,它將適應所有已知的iPhone和iPad。 每當特征集合發生變化時,它也會更新大小,因此它支持所有新的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.