首页 > 学院 > 开发设计 > 正文

Swift与OC混编

2019-11-08 00:20:11
字体:
来源:转载
供稿:网友

Swift与OC混编第一天

首先是swift调用oc,在swift的AppDelegate中使用oc控制器注意:必须先把oc头文件导入到一个叫 "项目名称-Bridging-Header"中然后再代理中进行调用 oc控制器 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. UIView.appearance().tintColor = UIColor.blackColor() window = UIWindow(frame: UIScreen.mainScreen().bounds) window?.backgroundColor = UIColor.whiteColor() window?.makeKeyAndVisible() UITabBar.appearance().translucent = false UITabBar.appearance().backgroundColor = UIColor.blackColor() UITabBar.appearance().tintColor = UIColor.blackColor() // 设置视图主窗口 self.window?.rootViewController = makeRootViewController() return true } func makeRootViewController() -> UITabBarController { let tabBarVC = UITabBarController() tabBarVC.viewControllers = makeChildViewControllers() tabBarVC.selectedIndex = 0 return tabBarVC } func makeChildViewControllers() -> [UIViewController] { let cons: [(UIViewController.Type, String, String)] = [(OneController.self, "Home".localized(), "Home"), (TreeController.self, "Statistics".localized(), "Dashboard")] return cons.map { let vc = UINavigationController(rootViewController: $0.init()) vc.tabBarItem = UITabBarItem(title: $1, image: $2.originalImage, selectedImage: $2.templateImage) return vc } }// 其中localized()和templateImage是写了一个工具类

localized

//// Strings.swift//import Foundation/// Internal current language keylet LCLCurrentLanguageKey = "LCLCurrentLanguageKey"/// Default language. English. If English is unavailable defaults to base localization.let LCLDefaultLanguage = "en"/// Name for language change notificationpublic let LCLLanguageChangeNotification = "LCLLanguageChangeNotification"public extension String { /** Swift 2 friendly localization syntax, replaces NSLocalizedString - Returns: The localized string. */ public func localized() -> String { if let path = NSBundle.mainBundle().pathForResource(Localize.currentLanguage(), ofType: "lPRoj"), bundle = NSBundle(path: path) { return bundle.localizedStringForKey(self, value: nil, table: nil) }else if let path = NSBundle.mainBundle().pathForResource("Base", ofType: "lproj"), bundle = NSBundle(path: path) { return bundle.localizedStringForKey(self, value: nil, table: nil) } return self } /** Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString) - Returns: The formatted localized string with arguments. */ public func localizedFormat(arguments: CVarArgType...) -> String { return String(format: localized(), arguments: arguments) } /** Swift 2 friendly plural localization syntax with a format argument - parameter argument: Argument to determine pluralisation - returns: Pluralized localized string. */ public func localizedPlural(argument: CVarArgType) -> String { return NSString.localizedStringWithFormat(localized(), argument) as String }}// MARK: Language Setting Functionspublic class Localize: NSObject { /** List available languages - Returns: Array of available languages. */ public class func availableLanguages() -> [String] { return NSBundle.mainBundle().localizations } /** Current language - Returns: The current language. String. */ public class func currentLanguage() -> String { if let currentLanguage = NSUserDefaults.standardUserDefaults().objectForKey(LCLCurrentLanguageKey) as? String { return currentLanguage } return defaultLanguage() } /** Change the current language - Parameter language: Desired language. */ public class func setCurrentLanguage(language: String) { let selectedLanguage = availableLanguages().contains(language) ? language : defaultLanguage() if (selectedLanguage != currentLanguage()){ NSUserDefaults.standardUserDefaults().setObject(selectedLanguage, forKey: LCLCurrentLanguageKey) NSUserDefaults.standardUserDefaults().synchronize() NSNotificationCenter.defaultCenter().postNotificationName(LCLLanguageChangeNotification, object: nil) } } /** Default language - Returns: The app's default language. String. */ public class func defaultLanguage() -> String { var defaultLanguage: String = String() guard let preferredLanguage = NSBundle.mainBundle().preferredLocalizations.first else { return LCLDefaultLanguage } let availableLanguages: [String] = self.availableLanguages() if (availableLanguages.contains(preferredLanguage)) { defaultLanguage = preferredLanguage } else { defaultLanguage = LCLDefaultLanguage } return defaultLanguage } /** Resets the current language to the default */ public class func resetCurrentLanguageToDefault() { setCurrentLanguage(self.defaultLanguage()) } /** Get the current language's display name for a language. - Parameter language: Desired language. - Returns: The localized string. */ public class func displayNameForLanguage(language: String) -> String { let locale : NSLocale = NSLocale(localeIdentifier: currentLanguage()) if let displayName = locale.displayNameForKey(NSLocaleLanguageCode, value: language) { return displayName } return String() }}

templateImage

//// Images.swift//import Foundationpublic extension String { public var image: UIImage? { return UIImage(named: self) } public var templateImage: UIImage? { return UIImage(named: self)?.imageWithRenderingMode(.AlwaysTemplate) } public var originalImage: UIImage? { return UIImage(named: self)?.imageWithRenderingMode(.AlwaysOriginal) }}

但是还有一个问题就是 图片选中的时候不能选择到选中的状态。 下篇解决。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表