如何为按钮创建滑动菜单栏?

时间:2011-08-05 11:47:42

标签: iphone objective-c xcode

大家好,我正在做一个应用程序,我将在菜单中实现按钮的滑动。像滑块这样包含许多按钮。

我浏览了以下链接

http://blog.sallarp.com/iphone-sliding-menu/

在上面的链接中,他们已经在Appdelegate方法中设置了所有内容...而在我的应用程序中有许多视图将继续进一步..所以任何人都可以帮助我如何实现按钮的滑动菜单栏?

谢谢你。

2 个答案:

答案 0 :(得分:1)

基本上,您所引用的滑动菜单由包含UIButtons作为子视图的UIScrollView组成。将滚动视图的contentSize设置为CGSizeMake(按钮的总宽度,一个按钮的高度)和enableScrolling为YES。

创建一个控制菜单的视图控制器可能是个好主意。此VC既可以生成滚动视图,也可以生成按钮,并作为按钮操作的目标。

答案 1 :(得分:1)

·H

IBOutlet UIScrollView *scrollView;

@property ( nonatomic , retain )  IBOutlet UIScrollView *scrollView;

-(void)AppleVijayAtFacebookDotCom:(id)sender;

-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons;

的.m

@synthesize scrollView;



-(void)AppleVijayAtFacebookDotCom:(id)sender{


    NSLog(@"AppleVijayAtFacebookDotCom called");


    UIButton *button=(UIButton *)sender;


    if (button.tag == 0) {

        NSLog(@"hey have clicked first button, this is my tag : %i \n\n",button.tag);
    }
    else if (button.tag == 1) {

        NSLog(@"hey have clicked second button, this is my tag : %i \n\n",button.tag);

    }
    // ......like this

    NSLog(@"button clicked is : %iBut \n\n",button.tag);



}       



-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons{

for (int i = 0; i < totalNoOfButtons; i++) {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button addTarget:self action:@selector(AppleVijayAtFacebookDotCom:) forControlEvents:UIControlEventTouchUpInside];

        //[button1 setImage:[UIImage imageNamed:@"Button.png"] forState:UIControlStateNormal];//with image

        //OR

    [button setTitle:[NSString stringWithFormat:@"%iBut",i] forState:UIControlStateNormal];//with title

    button.frame = CGRectMake(i*(offset+buttonSize.width), 8.0, buttonSize.width, buttonSize.height);

    button.clipsToBounds = YES;

    button.showsTouchWhenHighlighted=YES;

    button.layer.cornerRadius = 10;//half of the width

    button.layer.borderColor=[UIColor redColor].CGColor;

    button.layer.backgroundColor=[UIColor blackColor].CGColor;

    button.layer.borderWidth=2.0f;

    button.tag=i;

    [self.scrollView addSubview:button];

}

self.scrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);

    //self.navigationItem.titleView=self.scrollView;//if u have navigationcontroller then enable this line



    }

不要忘记在界面构建器中连接scrollView

在IB中创建滚动视图时确保你的scrollView高度为44.这是导航栏的默认值。所以它看起来不错。

in viewDidLoad call 

[self createMenuWithButtonSize:CGSizeMake(70.0, 30.0) withOffset:20.0f noOfButtons:30];

<强>输出

enter image description here