Emm*_*Emm 4 tabbar uitabbarcontroller swift xcode7
我正在使用标签栏,我有2个颜色问题.
第一个问题,色调颜色为灰色,我使用了一些代码将其更改为白色但仅在按下选项卡时才变为白色.
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let barColor = UIColor(red: 49/255, green: 75/255, blue: 108/255, alpha: 1.0)
let pressedTintColor = UIColor.whiteColor()
UITabBar.appearance().barTintColor = barColor
UITabBar.appearance().tintColor = pressedTintColor
return true
}
Run Code Online (Sandbox Code Playgroud)
第二个问题,按下的标签的背景颜色应该改变,但它没有改变.
(第1张图片在Xcode Simulator中就像测试一样,第2张图片就是它的设计,因此对图片和标签文字的重要性并不重要)
因此,假设所有选项卡始终为白色,并且当按下选项卡时仅更改选项卡的背景颜色.
要解决你AppDelegate
这样做的问题:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
UITabBar.appearance().tintColor = UIColor.whiteColor()
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)
return true
}
Run Code Online (Sandbox Code Playgroud)
在你ViewController
做这样的事情:
extension UIImage {
func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRectMake(0, 0, size.width, size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext()! as CGContextRef
CGContextTranslateCTM(context, 0, self.size.height)
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, CGBlendMode.Normal)
let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
CGContextClipToMask(context, rect, self.CGImage)
tintColor.setFill()
CGContextFillRect(context, rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
class FirstViewController: UIViewController {
var tabBar: UITabBar?
override func viewDidLoad() {
super.viewDidLoad()
tabBar = self.tabBarController!.tabBar
tabBar!.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar!.frame.width/CGFloat(tabBar!.items!.count), tabBar!.frame.height))
// To change tintColor for unselected tabs
for item in tabBar!.items! as [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Run Code Online (Sandbox Code Playgroud)
*第一个extension
到UIImage
:从另一个问题由同一作者采取如何更改标签栏项目的默认灰色?
归档时间: |
|
查看次数: |
3362 次 |
最近记录: |