如何在iOS中实现固定侧导航?

时间:2014-05-12 10:54:17

标签: ios

我是iOS开发的新手。我正在开发一个应用程序,其中包含所有ViewController左侧的固定侧边菜单。当用户从5中选择任何菜单时,所选菜单将以不同的颜色突出显示,但后面的部分会突出显示。

如何实现固定侧边菜单?

我被困在这上面了。我在Google上搜索了这么多网站,但没有任何帮助。有人可以提供示例代码吗?

任何帮助都将不胜感激。

谢谢&问候。

1 个答案:

答案 0 :(得分:2)

请尝试此代码。

首先创建一个UIView类型类。

编写此代码TabbarController.h

#import <UIKit/UIKit.h>

@class TabbarController;
@protocol Tabbar_protocol <NSObject>

@required
-(void)First_viewController;
-(void)Second_viewController;
-(void)Third_viewController;
-(void)Fourth_viewController;
-(void)Fifth_viewController;

@end

@interface TabbarController : UIView // this is your UIView type class

-(void)CreateTabbarController;
@property (nonatomic, assign) NSObject <Tabbar_protocol> *delegate;
@end

TabbarController.m

的代码
#import "TabbarController.h"

@implementation TabbarController
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
  {
     self = [super initWithFrame:frame];
      if (self) {
      // Initialization code
      [self CreateTabbarController];
   }
  return self;
}
-(void)CreateTabbarController
{
   UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
   [button1 setFrame:CGRectMake(0, 0, 50, 96)];
   [button1 setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
   [button1 setTag:101];
   [button1 addTarget:self action:@selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:button1];

   UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
   [button2 setFrame:CGRectMake(0, 96, 50, 96)];
   [button2 setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];
   [button2 setTag:102];
   [button2 addTarget:self action:@selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:button2];

   UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
   [button3 setFrame:CGRectMake(0, 192, 50, 96)];
   [button3 setBackgroundImage:[UIImage imageNamed:@"image3.png"] forState:UIControlStateNormal];
   [button3 setTag:103];
   [button3 addTarget:self action:@selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:button3];

   UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
   [button4 setFrame:CGRectMake(0, 288, 50, 96)];
   [button4 setBackgroundImage:[UIImage imageNamed:@"image4.png"] forState:UIControlStateNormal];
   [button4 setTag:104];
   [button4 addTarget:self action:@selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:button4];

   UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
   [button5 setFrame:CGRectMake(0, 384, 50, 96)];
   [button5 setBackgroundImage:[UIImage imageNamed:@"image5.png"] forState:UIControlStateNormal];
   [button5 setTag:105];
   [button5 addTarget:self action:@selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:button5];
}
-(void)TabbarBtn_Action:(id)sender
{
  switch ([sender tag])
  {
    case 101:
        if (self.delegate != nil)
        {
            // code for changing your button color
            [self.delegate First_viewController];
        }
        break;

    case 102:
        if (self.delegate != nil)
        {
            // code for changing your button color
            [self.delegate Second_viewController];
        }
        break;

    case 103:
        if (self.delegate != nil)
        {
            // code for changing your button color
            [self.delegate Third_viewController];
        }
        break;

    case 104:
        if (self.delegate != nil)
        {
            // code for changing your button color
            [self.delegate Fourth_viewController];
        }
        break;

    case 105:
        if (self.delegate != nil)
        {
            // code for changing your button color
            [self.delegate Fifth_viewController];
        }
        break;

    default:
        break;
  }
}
@end

在您的所有ViewController.h导入此课程&amp; ViewController.m类调用该委托方法。

#import "TabbarController.h"
@interface ViewController : UIViewController <Tabbar_protocol>
{

}

// ViewController.m class

-(void)First_viewController
{
    // Navigate to FirstViewController...
}
-(void)Second_viewController
{
    // Navigate to SecondViewController...
}
-(void)Third_viewController
{
    // Navigate to ThirdViewController...
}
-(void)Fourth_viewController
{
    // Navigate to FourthViewController...
}
-(void)Fifth_viewController
{
    // Navigate to FifthViewController...
}

像这样你必须在所有5 ViewController中打电话。