我必须使用XIB创建一个自定义可滚动标签栏控制器,我有一个不能使用storyboard的约束。我可以通过添加UIView开发自定义tabbar,在它下面我们有scrollview,然后有四个按钮作为tabbar按钮。然后当我尝试从一个控制器导航到另一个我获得导航按钮时。但我不需要那个。这就是我现在取得的成就。
但是当我从一个视图控制器切换到另一个视图控制器时,我想要像普通的标签栏控制器一样并且没有动画。自定义选项卡控制器应该像普通的tabbar控制器一样,但有两个约束 它应该是可滚动的 2.项目不应使用故事板。
这是我为app delegate和自定义标签栏控制器编写的代码。
Appdelegate
#import "AppDelegate.h"
#import "CustomTabBarControllerViewController.h"
#import "LoginViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Init YourViewController
//CustomTabBarControllerViewController *viewController = [[CustomTabBarControllerViewController alloc] init];
LoginViewController *viewController = [[LoginViewController alloc] init];
// Init YourNavigationController
UINavigationController *navigationContoller = [[UINavigationController alloc] initWithRootViewController: viewController];
// Set rootViewController
// self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.rootViewController = navigationContoller;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
}
#pragma mark - Core Data stack
@synthesize persistentContainer = _persistentContainer;
- (NSPersistentContainer *)persistentContainer {
// The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
@synchronized (self) {
if (_persistentContainer == nil) {
_persistentContainer = [[NSPersistentContainer alloc] initWithName:@"CustomTabBar"];
[_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
if (error != nil) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
NSLog(@"Unresolved error %@, %@", error, error.userInfo);
abort();
}
}];
}
}
return _persistentContainer;
}
#pragma mark - Core Data Saving support
- (void)saveContext {
NSManagedObjectContext *context = self.persistentContainer.viewContext;
NSError *error = nil;
if ([context hasChanges] && ![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, error.userInfo);
abort();
}
}
@end
这是我的自定义标签栏控制器
// CustomTabBarControllerViewController.m // CustomTabBar
#import "CustomTabBarControllerViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface CustomTabBarControllerViewController ()
@end
@implementation CustomTabBarControllerViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSBundle mainBundle] loadNibNamed:@"CustomTabBarControllerViewController" owner:self options:nil];
_customUiView.frame=CGRectMake(0, self.view.frame.size.height-49, self.view.frame.size.width, 49);
[self.view addSubview:_customUiView];
// _firstButtonOutlet.layer.borderWidth = 2.0f;
// _firstButtonOutlet.layer.borderColor = [UIColor blackColor].CGColor;
_customScrollView.contentSize=CGSizeMake(_firstButtonOutlet.frame.size.width*6, 0);
[self UnSelectAllTheTabs];
}
-(void)UnSelectAllTheTabs
{
_firstButtonOutlet.backgroundColor=[UIColor lightGrayColor];
_secondButtonOutlet.backgroundColor=[UIColor lightGrayColor];
_thirdButtonOutlet.backgroundColor=[UIColor lightGrayColor];
_fourthButtonOutlet.backgroundColor=[UIColor lightGrayColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//- (IBAction)didTapBarButton:(UIButton *)sender {
//
// [self UnSelectAllTheTabs];
// switch (sender.tag) {
// case 0:
// {
// //_firstButtonOutlet.backgroundColor=[UIColor grayColor];
//// [[NSBundle mainBundle] loadNibNamed:@"FirstViewController" owner:self options:nil];
// // self.selectedIndex=0;
//
// FirstViewController *fc = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
// [self.navigationController pushViewController:fc animated:YES];
//
// _firstButtonOutlet.backgroundColor= [UIColor grayColor];
// self.selectedIndex=0;
// break;
// }
// case 1:
// {
//
//
// SecondViewController *sv = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
// [self.navigationController pushViewController:sv animated:YES];
//
// _secondButtonOutlet.backgroundColor=[UIColor blackColor];
//// [[NSBundle mainBundle] loadNibNamed:@"SecondViewController" owner:self options:nil];
// self.selectedIndex=1;
// break;
// }
// case 2:
// {
//
// ThirdViewController *tv = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];
// _thirdButtonOutlet.backgroundColor=[UIColor grayColor];
// // [self.navigationController pushViewController:tv animated:YES]
//// [[NSBundle mainBundle] loadNibNamed:@"ThirdViewController" owner:self options:nil];
// self.selectedIndex=2;
// break;
// }
// case 3:
// {
// // FourthViewController *fv = [[FourthViewController All]]
// _fourthButtonOutlet.backgroundColor=[UIColor grayColor];
//// // [[NSBundle mainBundle] loadNibNamed:@"FourthViewController" owner:self options:nil];
// self.selectedIndex=3;
// break;
// }
//
// default:
// break;
// }
//
//}
- (IBAction)firstButtonClicked:(UIButton *)sender {
_firstButtonOutlet.backgroundColor= [UIColor grayColor];
_secondButtonOutlet.backgroundColor=[UIColor lightGrayColor];
_thirdButtonOutlet.backgroundColor=[UIColor lightGrayColor];
_fourthButtonOutlet.backgroundColor=[UIColor lightGrayColor];
FirstViewController *fc = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
[self.navigationController pushViewController:fc animated:NO];
// [self.navigationController showViewController:fc sender:nil];
//
}
- (IBAction)secondButtonClicked:(UIButton *)sender {
_secondButtonOutlet.backgroundColor= [UIColor grayColor];
_thirdButtonOutlet.backgroundColor=[UIColor lightGrayColor];
_fourthButtonOutlet.backgroundColor=[UIColor lightGrayColor];
_firstButtonOutlet.backgroundColor=[UIColor lightGrayColor];
SecondViewController *sv = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:sv animated:NO];
}
- (IBAction)thirdButtonClicked:(UIButton *)sender {
_secondButtonOutlet.backgroundColor = [UIColor lightGrayColor];
_fourthButtonOutlet.backgroundColor = [UIColor lightGrayColor];
_firstButtonOutlet.backgroundColor=[UIColor lightGrayColor];
_thirdButtonOutlet.backgroundColor= [UIColor grayColor];
ThirViewController *tv = [[ThirViewController alloc]initWithNibName:@"ThirViewController" bundle:nil];
[self.navigationController pushViewController:tv animated:NO];
}
- (IBAction)fourthButtonClicked:(UIButton *)sender {
_firstButtonOutlet.backgroundColor = [UIColor lightGrayColor];
_secondButtonOutlet.backgroundColor = [UIColor lightGrayColor];
_thirdButtonOutlet.backgroundColor= [UIColor lightGrayColor];
_fourthButtonOutlet.backgroundColor = [UIColor grayColor];
FourthViewController *fv = [[FourthViewController alloc]initWithNibName:@"FourthViewController" bundle:nil];
[self.navigationController pushViewController:fv animated:NO];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
答案 0 :(得分:2)
你几乎实现了目标。您的代码只有一个问题。
**搜索正确的格式,我在swift工作,没有客观的c代码。
- (IBAction)fourthButtonClicked:(UIButton *)sender {
_firstButtonOutlet.backgroundColor = [UIColor lightGrayColor];
_secondButtonOutlet.backgroundColor = [UIColor lightGrayColor];
_thirdButtonOutlet.backgroundColor= [UIColor lightGrayColor];
_fourthButtonOutlet.backgroundColor = [UIColor grayColor];
FourthViewController *fv = [[FourthViewController alloc]initWithNibName:@"FourthViewController" bundle:nil];
// [self.navigationController pushViewController:fv animated:NO];
fv.frame = CGRectMake(x:0,y:0,self.view.frame.origin.width,self.view.frame.origin.height - (Your tabbar height));
//Check if any other view controller is already added to it. Remove that from view and super.
[self.view addSubview: fv.view];
[self addChildViewController:fv];
}