iOS:制作左右側滑(抽屜式)菜單


感謝控件作者:https://github.com/SocialObjects-Software/AMSlideMenu

首先上效果圖:


這里我們使用AMSlideMenu來實現左右側滑菜單的效果。控件支持單獨左側滑、單獨右側滑和左右側滑。同時支持Storyboard和xib兩種開發模式。這里介紹第二種,在xib中的開發。
開發步驟如下:
1.    在Podfile中添加:pod "AMSlideMenu", "~> 1.5.3",通過pod install導入項目。
2.    在Pods項目中(注意:不是你自己的項目),在Pods-*.pch文件中添加如下一行代碼:

// 必須,否則xib方式會報錯
#define AMSlideMenuWithoutStoryboards

3.    實現一個繼承了AMSlideMenuMainViewController類的ViewController。主要代碼如下:

- (void)viewDidLoad
{
   /*******************************
    *     初始化菜單
    *******************************/
    self.leftMenu = [[LeftMenuTVC alloc] initWithNibName:@"LeftMenuTVC" bundle:nil];
    self.rightMenu = [[RightMenuTVC alloc] initWithNibName:@"RightMenuTVC" bundle:nil];
   /*******************************
    *     結束初始化
    *******************************/

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

// 設置左側菜單按鈕樣式(右側按鈕類似操作)
- (void)configureLeftMenuButton:(UIButton *)button
{
    CGRect frame = button.frame;
    frame.origin = (CGPoint){0,0};
    frame.size = (CGSize){40,40};
    button.frame = frame;
    
    [button setImage:[UIImage imageNamed:@"icon-menu.png"] forState:UIControlStateNormal];
}

4.    實現一個繼承了AMSlideMenuLeftTableViewController的UITableViewController的類作為左側菜單(右側菜單類似)
主要代碼如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // 初始化菜單項
    self.tableData = [@[@"VC 1",@"VC 2",@"VC 3"] mutableCopy];
}

// 點擊菜單項跳轉到不同的VC
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *nvc;
    UIViewController *rootVC;
    switch (indexPath.row) {
        case 0:
        {
            rootVC = [[FirstVC alloc] initWithNibName:@"FirstVC" bundle:nil];
        }
            break;
        case 1:
        {
            rootVC = [[SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
        }
            break;
        case 2:
        {
            rootVC = [[ThirdVC alloc] initWithNibName:@"ThirdVC" bundle:nil];
        }
            break;
        
        default:
            break;
    }
    nvc = [[UINavigationController alloc] initWithRootViewController:rootVC];
    
    [self openContentNavigationController:nvc];
}

5.    最后記得在AppDelegate中要做這步操作(當然,其它地方也可以):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MainVC *mainVC = [[MainVC alloc] init];

    UINavigationController *startNVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.rootViewController = startNVC;
    [self.window makeKeyAndVisible];
    
    return YES;
}


有不明白的地方可以直接下載源碼。源碼地址:http://files.cnblogs.com/ilovewindy/AMSlideMenu.zip


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM