### Install VTMagic with CocoaPods Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Add this line to your Podfile to install VTMagic. Remember to import afterwards. ```ruby pod "VTMagic" ``` -------------------------------- ### Import VTMagic Manually Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md If installing manually, import the VTMagic.h file into your project. ```objective-c #import "VTMagic.h" ``` -------------------------------- ### Get Specified View Controller by Page Index Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Retrieve a view controller for a specific page index from the magic view or magic controller. Returns nil if the page is not currently on screen. ```objective-c UIViewController *viewController = [self.magicView viewControllerAtPage:3]; UIViewController *viewController = [self.magicController viewControllerAtPage:3]; ``` -------------------------------- ### Initialize VTMagicController with Custom Properties Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Configure VTMagicController properties like navigation color, slider color, layout style, switch style, and navigation height. Set the data source and delegate. ```objective-c - (VTMagicController *)magicController { if (!_magicController) { _magicController = [[VTMagicController alloc] init]; _magicController.magicView.navigationColor = [UIColor whiteColor]; _magicController.magicView.sliderColor = [UIColor redColor]; _magicController.magicView.layoutStyle = VTLayoutStyleDivide; _magicController.magicView.switchStyle = VTSwitchStyleDefault; _magicController.magicView.navigationHeight = 40.f; _magicController.magicView.dataSource = self; _magicController.magicView.delegate = self; } return _magicController; } ``` -------------------------------- ### Handle View Appearance Callbacks Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Implement these methods to perform actions when a page appears or disappears. Useful for refreshing page data or performing cleanup. ```objective-c - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // do something... } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // do something... } ``` -------------------------------- ### Implement VTMagicReuseProtocol: Prepare for Reuse Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md This method is called when a page is being reused. It's the place to clear old data or reset content offsets. ```objective-c - (void)vtm_prepareForReuse { NSLog(@"clear old data if needed:%@", self); } ``` -------------------------------- ### Implement VTMagicViewDataSource: Menu Item Configuration Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Configure the appearance and behavior of each menu item button. Reusable items are dequeued for efficiency. ```objective-c - (UIButton *)magicView:(VTMagicView *)magicView menuItemAtIndex:(NSUInteger)itemIndex { static NSString *itemIdentifier = @"itemIdentifier"; UIButton *menuItem = [magicView dequeueReusableItemWithIdentifier:itemIdentifier]; if (!menuItem) { menuItem = [UIButton buttonWithType:UIButtonTypeCustom]; [menuItem setTitleColor:RGBCOLOR(50, 50, 50) forState:UIControlStateNormal]; [menuItem setTitleColor:RGBCOLOR(169, 37, 37) forState:UIControlStateSelected]; menuItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:16.f]; } return menuItem; } ``` -------------------------------- ### Switch to a Specified Page Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Programmatically switch to a specific page using either the magic view or magic controller. Supports animated transitions. ```objective-c [self.magicView switchToPage:3 animated:YES]; [self.magicController switchToPage:3 animated:YES]; ``` -------------------------------- ### Implement VTMagicViewDelegate: View Did Appear Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md This delegate method is called when a view controller appears on screen. It provides the view controller and its page index. ```objective-c - (void)magicView:(VTMagicView *)magicView viewDidAppear:(UIViewController *)viewController atPage:(NSUInteger)pageIndex { NSLog(@"pageIndex:%ld viewDidAppear:%@", (long)pageIndex, viewController.view); } ``` -------------------------------- ### Implement VTMagicViewDataSource: View Controller for Page Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Provide the view controller for a given page index. Reusable view controllers are dequeued for efficiency. ```objective-c - (UIViewController *)magicView:(VTMagicView *)magicView viewControllerAtPage:(NSUInteger)pageIndex { if (0 == pageIndex) { static NSString *recomId = @"recom.identifier"; VTRecomViewController *recomViewController = [magicView dequeueReusablePageWithIdentifier:recomId]; if (!recomViewController) { recomViewController = [[VTRecomViewController alloc] init]; } return recomViewController; } static NSString *gridId = @"grid.identifier"; VTGridViewController *gridViewController = [magicView dequeueReusablePageWithIdentifier:gridId]; if (!gridViewController) { gridViewController = [[VTGridViewController alloc] init]; } return gridViewController; } ``` -------------------------------- ### Subclass VTMagicController and Configure Properties Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Alternatively, subclass VTMagicController and configure its magicView properties directly in viewDidLoad. Ensure you reload data. ```objective-c #import "VTMagicController.h" @interface ViewController : VTMagicController @end ``` ```objective-c @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.magicView.navigationColor = [UIColor whiteColor]; self.magicView.sliderColor = [UIColor redColor]; self.magicView.layoutStyle = VTLayoutStyleDefault; self.magicView.switchStyle = VTSwitchStyleDefault; self.magicView.navigationHeight = 40.f; self.magicView.dataSource = self; self.magicView.delegate = self; [self.magicView reloadData]; } ``` -------------------------------- ### Implement VTMagicViewDataSource: Menu Titles Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Provide the array of menu titles for the VTMagicView. This method must be implemented. ```objective-c - (NSArray *)menuTitlesForMagicView:(VTMagicView *)magicView { return _menuList; } ``` -------------------------------- ### Access VTMagic Controller Properties Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Retrieve the nearest magic controller to access properties like the current page index or view controller. Ensure VTMagic.h is imported. ```objective-c NSInteger currentPage = self.magicController.currentPage; UIViewController *viewController = self.magicController.currentViewController; ``` -------------------------------- ### Implement VTMagicViewDelegate: Did Select Item Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md This delegate method is called when a menu item is selected. It provides the index of the selected item. ```objective-c - (void)magicView:(VTMagicView *)magicView didSelectItemAtIndex:(NSUInteger)itemIndex { NSLog(@"didSelectItemAtIndex:%ld", (long)itemIndex); } ``` -------------------------------- ### Implement VTMagicViewDelegate: View Did Disappear Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md This delegate method is called when a view controller disappears from screen. It provides the view controller and its page index. ```objective-c - (void)magicView:(VTMagicView *)magicView viewDidDisappear:(UIViewController *)viewController atPage:(NSUInteger)pageIndex { NSLog(@"pageIndex:%ld viewDidDisappear:%@", (long)pageIndex, viewController.view); } ``` -------------------------------- ### Integrate VTMagicController into a View Controller Source: https://github.com/tianzhuo112/vtmagic/blob/master/README.md Add the VTMagicController as a child view controller and add its view to the main view. Reload data to display the pages. ```objective-c - (void)viewDidLoad { [super viewDidLoad]; [self addChildViewController:self.magicController]; [self.view addSubview:_magicController.view]; [_magicController.magicView reloadData]; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.