【问题标题】:how to create a sliding menu bar for buttons?如何为按钮创建滑动菜单栏?
【发布时间】:2011-10-20 19:24:57
【问题描述】:

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

我浏览了以下链接

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

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

谢谢你。

【问题讨论】:

标签: iphone objective-c xcode


【解决方案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 中创建滚动视图时,确保您的滚动视图高度为 44。这是导航栏的默认值。所以它看起来不错。

in viewDidLoad call 

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

输出

【讨论】:

  • 非常感谢..但我在背景颜色、边框颜色和所有它的出现都没有在对象类型 CAlayer 上找到错误..所以我要做什么?
  • #import #import #import
  • 不要忘记在界面生成器中连接scrollView
  • 是的,我添加了,但是当我运行它时,我得到一个空白屏幕..我不知道我在哪里连接滚动视图?
  • 从界面生成器中拖动 uiscrollview 然后在 .h 文件中连接你的 scrollView ivar。小心。
【解决方案2】:

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

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

【讨论】:

  • 感谢您的回复...因为我是 iphone 开发的新手,所以我不太了解它..您能详细说明一下或提供一些示例
猜你喜欢
  • 1970-01-01
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 2015-09-18
相关资源
最近更新 更多