2016-12-22 5 views
0
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 
UITabBar *tabBar = tabBarController.tabBar; 
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0]; 
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1]; 
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2]; 
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3]; 

tabBarItem1.title = @"Home"; 
tabBarItem2.title = @"Maps"; 
tabBarItem3.title = @"My Plan"; 
tabBarItem4.title = @"Settings"; 

[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]]; 
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"maps.png"]]; 
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"myplan_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"myplan.png"]]; 
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"settings_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"settings.png"]]; 


// Change the tab bar background 
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"]; 
[[UITabBar appearance] setBackgroundImage:tabBarBackground]; 
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]]; 

// Change the title color of tab bar items 
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                [UIColor whiteColor], UITextAttributeTextColor, 
                nil] forState:UIControlStateNormal]; 
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0]; 
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                titleHighlightedColor, UITextAttributeTextColor, 
                nil] forState:UIControlStateHighlighted]; 


return YES; 

Я нашел этот код в объекте C. Мне нужно то же самое в swift. Как я могу это получить. Пожалуйста, помогите, поскольку я новичок в iOS.Мне нужно добавить пользовательский контроллер таблеток в appdelegate swift

Я пытаюсь создать пользовательскую вкладку с помощью swift. Вышеприведенный код является объективным кодом c, написанным в appdelegate.

+0

[объективистские tivec2Swift] (https://objectivec2swift.com/) –

+0

Я отредактировал вопрос – Rakesh

+0

№. Панель вкладок не отображается. Я не могу перейти с моей страницы на следующую страницу. – Rakesh

ответ

0

Попробуйте

var tabBarController = (self.window!.rootViewController! as! UITabBarController) 
var tabBar = tabBarController.tabBar 
var tabBarItem1 = tabBar.items()[0] 
var tabBarItem2 = tabBar.items()[1] 
var tabBarItem3 = tabBar.items()[2] 
var tabBarItem4 = tabBar.items()[3] 
tabBarItem1.title = "Home" 
tabBarItem2.title = "Maps" 
tabBarItem3.title = "My Plan" 
tabBarItem4.title = "Settings" 

tabBarItem1?.image = UIImage(named: "home.png")! 
tabBarItem1?.selectedImage = UIImage(named: "home_selected.png")! 
tabBarItem2?.image = UIImage(named: "maps.png")! 
tabBarItem2?.selectedImage = UIImage(named: "maps_selected.png")! 
// same for tabBarItem3,tabBarItem4 so on .... 

// Change the tab bar background 
var tabBarBackground = UIImage(named: "tabbar.png")! 
UITabBar.appearance().backgroundImage = tabBarBackground 
UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabbar_selected.png")! 
// Change the title color of tab bar items 
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal) 
var titleHighlightedColor = UIColor(red: CGFloat(153/255.0), green: CGFloat(192/255.0), blue: CGFloat(48/255.0), alpha: CGFloat(1.0)) 
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : titleHighlightedColor], for: .highlighted) 
+0

Для этого кода tabBarItem1? .setFinishedSelectedImage (UIImage (имя: «home_selected.png») !, withFinishedUnselectedImage: UIImage (named: «home.png»)!) tabBarItem2 ?. – Rakesh

+0

Это показывает 'setFinishedSelectedImage (_: withFinishedUnselectedImage :) недоступен: Использование initWithTitle: Изображение: selectedImage: или изображения и selectedImage свойства наряду с UIImageRenderingModeAlwaysOriginal – Rakesh

+0

вы можете напрямую дать название, изображение и выбранное изображение из раскадровки –

0

Старинное Свифта 3:

let tabBarController = (self.window!.rootViewController! as! UITabBarController) 
let tabBar = tabBarController.tabBar 
let tabBarItem1 = tabBar.items?[0] 
let tabBarItem2 = tabBar.items?[1] 
let tabBarItem3 = tabBar.items?[2] 
let tabBarItem4 = tabBar.items?[3] 
tabBarItem1?.title = "Home" 
tabBarItem2?.title = "Maps" 
tabBarItem3?.title = "My Plan" 
tabBarItem4?.title = "Settings" 

tabBarItem1?.image = UIImage(named: "home_selected.png") 
tabBarItem2?.image = UIImage(named: "home_selected.png") 
tabBarItem3?.image = UIImage(named: "home_selected.png") 
tabBarItem4?.image = UIImage(named: "home_selected.png") 

tabBarItem1?.selectedImage = UIImage(named: "home_selected.png")?.withRenderingMode(.alwaysOriginal) 
tabBarItem2?.selectedImage = UIImage(named: "maps_selected.png")?.withRenderingMode(.alwaysOriginal) 
tabBarItem3?.selectedImage = UIImage(named: "myplan_selected.png")?.withRenderingMode(.alwaysOriginal) 
tabBarItem3?.selectedImage = UIImage(named: "settings_selected.png")?.withRenderingMode(.alwaysOriginal) 

// Change the tab bar background 
let tabBarBackground = UIImage(named: "tabbar.png")! 
UITabBar.appearance().backgroundImage = tabBarBackground 
UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabbar_selected.png")! 

// Change the title color of tab bar items 
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal) 

let titleHighlightedColor = UIColor(red: CGFloat(153/255.0), green: CGFloat(192/255.0), blue: CGFloat(48/255.0), alpha: CGFloat(1.0)) 
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal) 
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: titleHighlightedColor], for: .highlighted) 

EDIT:

Набор RootViewController:

let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate 
appDelegate.window?.backgroundColor = UIColor.white 
appDelegate.window?.rootViewController = MyVIewCOntroller() 
appDelegate.window?.makeKeyAndVisible() 
+0

@Rakesh Разве вы не используете раскадровку? –

+0

Я использую xib. – Rakesh

+0

Не могли бы вы показать мне, как установить контроллер rootview в tabbarcontroller? Мне нужно установить rootview в мой класс «MyVIewCOntroller». – Rakesh

Смежные вопросы