如何使用目标c在iphone中添加UITabBar

Vis*_*hnu 6 iphone objective-c uitabbar

如何以编程方式为iphone应用添加添加UITabBar.需要一些建议和示例代码.

Ash*_*hok 28

这两方面......

第一个方面(UITabBarController):这里你可以在UITabBarController中添加多个类(即Viewcontrollers),如前面的答案所示......

第二个方面(UITabbar):在这里您可以在任何视图控制器中添加Tabbar ...对于此Tabbar,您可以添加多个UITabBar项目...并且您可以获得单独的Tabbar项目操作.....

请参阅UITabBar的以下代码

---------> 添加UITabBar

UITabBar *myTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
myTabBar.delegate=self;   //here you need import the protocol <UITabBarDelegate>
[self.view addSubview:myTabBar];
Run Code Online (Sandbox Code Playgroud)

---------> 向UITabBar添加项目

NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];

UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Item1" image:[UIImage imageNamed:@"Item1image.png"] tag:0];
UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1 ];
UITabBarItem *tabBarItem3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2 ];

[tabBarItems addObject:tabBarItem1];
[tabBarItems addObject:tabBarItem2];
[tabBarItems addObject:tabBarItem3];

myTabBar.items = tabBarItems;
myTabBar.selectedItem = [tabBarItems objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

---------> 在委托方法中获取项目操作

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger selectedTag = tabBar.selectedItem.tag;
    NSLog(@"%ld",(long)selectedTag);
    if (selectedTag == 0) {
        //Do what ever you want here
    } else if(selectedTag == 1) {
        //Do what ever you want
    } else { //if(selectedTag == 2)
        //Do what ever you want here
    }
}
Run Code Online (Sandbox Code Playgroud)



更新:(雨燕2.0)

1.第一方面(的UITabBarController):
这将允许我们配置UIViewControllerS作为tabbar中的项目UITabBarController.
以下是UITabBarController的基本方法:

--------->在appdelegate中将UITabBarController添加到窗口

let tabBarController = UITabBarController(nibName: nil, bundle: nil)
tabBarController.delegate = self // Need to import 'UITabBarControllerDelegate'
self.window!.rootViewController = tabBarController;
self.window?.makeKeyAndVisible();
Run Code Online (Sandbox Code Playgroud)

--------->配置UIViewControllers

let firstViewController = UIViewController()
firstViewController.title = "FirstVC"
firstViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let firstNavController: UINavigationController = UINavigationController(rootViewController: firstViewController)

let secondViewController = UIViewController()
secondViewController.title = "SecondVC"
secondViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let secondNavController: UINavigationController = UINavigationController(rootViewController: secondViewController)

let thirdViewController = UIViewController()
thirdViewController.title = "ThirdVC"
thirdViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let thirdNavController: UINavigationController = UINavigationController(rootViewController: thirdViewController)

let forthViewController = UIViewController()
forthViewController.title = "ForthVC"
forthViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)
let forthNavController: UINavigationController = UINavigationController(rootViewController: forthViewController)
Run Code Online (Sandbox Code Playgroud)

--------->将UIViewControllers附加到UITabBarController

tabBarController.viewControllers = [firstNavController, secondNavController, thirdNavController, forthNavController]
Run Code Online (Sandbox Code Playgroud)

--------->在'UITabBarControllerDelegate'中接收事件

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    print("didSelectViewController: \(viewController.title) ?")
}
Run Code Online (Sandbox Code Playgroud)


2.第二个方面(UITabBar):
这将允许我们创建tabbar项目.我们可以在委托方法中获取单个项目的事件
以下是UITabBar的基本方法:

--------->将UITabBar添加到视图

let frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 49, CGRectGetWidth(self.view.frame), 49)
let tabBar: UITabBar = UITabBar(frame: frame)
tabBar.delegate = self // Need to import 'UITabBarDelegate'
self.view.addSubview(tabBar)
Run Code Online (Sandbox Code Playgroud)

--------->准备TabbarItems

let tabBarItem1 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let tabBarItem2 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let tabBarItem3 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let tabBarItem4 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)
Run Code Online (Sandbox Code Playgroud)

--------->将UITabBarItems添加到UITabBar

tabBar.items = [tabBarItem1, tabBarItem2, tabBarItem3, tabBarItem4]
tabBar.selectedItem = tabBarItem1
Run Code Online (Sandbox Code Playgroud)

--------->在'UITabBarDelegate'中接收事件

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    let selectedTag:Int = (tabBar.selectedItem?.tag)!
    print(selectedTag)
    switch selectedTag {
    case 0:
        print("tabBarItem1")
        //Do what ever you want here
    case 1:
        print("tabBarItem2")
        //Do what ever you want here
    case 2:
        print("tabBarItem3")
        //Do what ever you want here
    default:
        print("tabBarItem4")
        //Do what ever you want here
    }
}
Run Code Online (Sandbox Code Playgroud)


Raj*_*mar 8

- (void) setUpTabBar {
    FirstViewController *firstViewController = [[FirstViewController alloc]init];
    firstViewController.title = @"First View";
    firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
    UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];

    SecondViewController *secondViewController = [[SecondViewController alloc]init];
    secondViewController.title = @"Second View";
    secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
    UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];

    ThirdViewController *thirdViewController = [[ThirdViewController alloc]init];
    thirdViewController.title = @"Third View";
    thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
    UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];

    ForthViewController *forthViewController = [[ForthViewController alloc]init];
    forthViewController.title = @"Forth View";
    forthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2];
    UINavigationController *forthNavController = [[UINavigationController alloc]initWithRootViewController:forthViewController];

    tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
    tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, forthNavController, nil];
    tabBarController.delegate = self;             
    [self sizeViewToAvailableWindow:[tabBarController view]];

    [firstNavController release];
    [firstViewController release];

    [secondNavController release];
    [secondViewController release];

    [thirdNavController release];
    [thirdViewController release];

    [forthNavController release];
    [forthViewController release];
}
Run Code Online (Sandbox Code Playgroud)

这是以编程方式制作TabBarController的代码.希望这会帮助你.