如何在导航栏上添加 uiscrollview?

发布于 2024-12-17 21:57:13 字数 3626 浏览 0 评论 0原文

可能的重复:
如何我在iPhone应用程序开发中在导航栏上添加超过10个按钮?

我想在包含八个按钮的导航栏上设置一个 UIScrollView,并且当我愿意时,该导航栏还包含 leftbaritem 和 rightbaritem按下一个或上一个按钮,然后滚动视图必须随此按钮滚动,因为某些按钮将超出滚动视图框架。

到目前为止,我已经制作了滚动视图和按钮,但问题是按钮现在正在显示。任何人都可以通过给出示例或发送源代码来帮助我。

预先感谢

编辑

topToolBar = [UIToolbar new];
[topToolBar sizeToFit];
topToolBar.frame = CGRectMake(0,0, 280, 50);
topToolBar.barStyle = UIBarStyleBlackTranslucent;

segmentedControllerView = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 280, 50)];
segmentedControllerView.clipsToBounds = YES;

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0,0, 60.0, segmentedControllerView.frame.size.height -10);
button.clipsToBounds = YES;
[segmentedControllerView addSubview:button];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 addTarget:self 
           action:@selector(aMethod2:)
 forControlEvents:UIControlEventTouchDown];
[button2 setTitle:@"Show View2" forState:UIControlStateNormal];
button2.frame = CGRectMake(60,0, 60.0, segmentedControllerView.frame.size.height - 10);
[segmentedControllerView addSubview:button2];

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 addTarget:self 
           action:@selector(aMethod3:)
 forControlEvents:UIControlEventTouchDown];
[button3 setTitle:@"Show View" forState:UIControlStateNormal];
button3.frame = CGRectMake(120,0, 60.0, segmentedControllerView.frame.size.height -10 );
[segmentedControllerView addSubview:button3];

UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button4 addTarget:self 
           action:@selector(aMethod4:)
 forControlEvents:UIControlEventTouchDown];
[button4 setTitle:@"Show View" forState:UIControlStateNormal];
button4.frame = CGRectMake(180,0, 60.0, segmentedControllerView.frame.size.height -10 );
[segmentedControllerView addSubview:button4];

[topToolBar addSubview:segmentedControllerView];
self.navigationItem.titleView = topToolBar;

我也接近结果,但问题是当我按下下一个或上一个按钮时,它会移动到下一个或上一个按钮,但它应该移动到视图下......这

是动画代码:

   -(void)previousBarButtonAction{

[UIView beginAnimations: @"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
segmentedControllerView.frame = CGRectMake(segmentedControllerView.frame.origin.x - 50,
                                   segmentedControllerView.frame.origin.y,
                                   segmentedControllerView.frame.size.width,
                                   segmentedControllerView.frame.size.height);
[UIView commitAnimations];


}
-(void)nextBarButtonAction{

[UIView beginAnimations: @"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
segmentedControllerView.frame = CGRectMake(segmentedControllerView.frame.origin.x + 50,
                              segmentedControllerView.frame.origin.y,
                              segmentedControllerView.frame.size.width,
                              segmentedControllerView.frame.size.height);
[UIView commitAnimations];

}

Possible Duplicate:
How can i add more than 10 buttons on a navigationbar in iphone application development?

I want to set an UIScrollView on a navigation bar which containing eight buttons and this navigation bar also containing leftbaritem and rightbaritem when i will press the next or previous button then scrollview have to scroll with this button because some of button will be out of the scrollview frame.

Until now i have made scroll view and buttons but problem is button are now displaying.Can anybody help me by giving an example or sending source code.

Thanks in Advance

EDIT

topToolBar = [UIToolbar new];
[topToolBar sizeToFit];
topToolBar.frame = CGRectMake(0,0, 280, 50);
topToolBar.barStyle = UIBarStyleBlackTranslucent;

segmentedControllerView = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 280, 50)];
segmentedControllerView.clipsToBounds = YES;

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0,0, 60.0, segmentedControllerView.frame.size.height -10);
button.clipsToBounds = YES;
[segmentedControllerView addSubview:button];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 addTarget:self 
           action:@selector(aMethod2:)
 forControlEvents:UIControlEventTouchDown];
[button2 setTitle:@"Show View2" forState:UIControlStateNormal];
button2.frame = CGRectMake(60,0, 60.0, segmentedControllerView.frame.size.height - 10);
[segmentedControllerView addSubview:button2];

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 addTarget:self 
           action:@selector(aMethod3:)
 forControlEvents:UIControlEventTouchDown];
[button3 setTitle:@"Show View" forState:UIControlStateNormal];
button3.frame = CGRectMake(120,0, 60.0, segmentedControllerView.frame.size.height -10 );
[segmentedControllerView addSubview:button3];

UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button4 addTarget:self 
           action:@selector(aMethod4:)
 forControlEvents:UIControlEventTouchDown];
[button4 setTitle:@"Show View" forState:UIControlStateNormal];
button4.frame = CGRectMake(180,0, 60.0, segmentedControllerView.frame.size.height -10 );
[segmentedControllerView addSubview:button4];

[topToolBar addSubview:segmentedControllerView];
self.navigationItem.titleView = topToolBar;

I am also close to the result but problem is when i am pressing the next or prev button then it moving over the next or prev button but it should be move under the view....

This is the animation code:

   -(void)previousBarButtonAction{

[UIView beginAnimations: @"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
segmentedControllerView.frame = CGRectMake(segmentedControllerView.frame.origin.x - 50,
                                   segmentedControllerView.frame.origin.y,
                                   segmentedControllerView.frame.size.width,
                                   segmentedControllerView.frame.size.height);
[UIView commitAnimations];


}
-(void)nextBarButtonAction{

[UIView beginAnimations: @"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
segmentedControllerView.frame = CGRectMake(segmentedControllerView.frame.origin.x + 50,
                              segmentedControllerView.frame.origin.y,
                              segmentedControllerView.frame.size.width,
                              segmentedControllerView.frame.size.height);
[UIView commitAnimations];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘亓 2024-12-24 21:57:13

我解决这个问题的方式略有不同......

viewDidLoad 中,我采用了 scrollView 并调用了我的函数

menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,40)];
menuScrollView.showsHorizontalScrollIndicator = FALSE;
menuScrollView.showsVerticalScrollIndicator = FALSE;
menuScrollView.bounces = TRUE;


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

然后在我的 function 中我有创建了我的菜单按钮,它看起来像导航栏上的按钮

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

NSLog(@"inserting into the function for menu bar button creation"); 
for (int i = 0; i < totalNoOfButtons; i++) {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button addTarget:self action:@selector(mybuttons:) forControlEvents:UIControlEventTouchUpInside];
    if(i==0){
    [button setTitle:[NSString stringWithFormat:@"Dashboard"] forState:UIControlStateNormal];//with title
    }
    else if(i==1){
        [button setTitle:[NSString stringWithFormat:@"Order"] forState:UIControlStateNormal];//with title
    }
    else if(i==2){
        [button setTitle:[NSString stringWithFormat:@"Product"] forState:UIControlStateNormal];//with title
    }
    else if(i==3){
        [button setTitle:[NSString stringWithFormat:@"Customers"] forState:UIControlStateNormal];//with title
    }
    else if(i==4){
        [button setTitle:[NSString stringWithFormat:@"Content"] forState:UIControlStateNormal];//with title
    }
    else if(i==5){
        [button setTitle:[NSString stringWithFormat:@"Site Analysis"] forState:UIControlStateNormal];//with title
    }
    else if(i==6){
        [button setTitle:[NSString stringWithFormat:@"Store Settings"] forState:UIControlStateNormal];//with title
    }
    else if(i==7){
        [button setTitle:[NSString stringWithFormat:@"CMS Settings"] 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 = 0;//half of the width
    //button.layer.borderColor=[UIColor clearColor];
    button.layer.backgroundColor=[UIColor blueColor].CGColor;
    button.titleLabel.font = [UIFont systemFontOfSize:10];
    button.layer.borderWidth=0.0f;
    button.tag=i;
    [menuScrollView addSubview:button];
}
menuScrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
[self.view addSubview:menuScrollView];

}

,它解决了...快乐编码...:))

I have solve this problem slightly different way....

In viewDidLoad I have take a scrollView and called my function

menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,40)];
menuScrollView.showsHorizontalScrollIndicator = FALSE;
menuScrollView.showsVerticalScrollIndicator = FALSE;
menuScrollView.bounces = TRUE;


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

Then in my function i have created my menu buttons which will be look like buttons on a navigation bar

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

NSLog(@"inserting into the function for menu bar button creation"); 
for (int i = 0; i < totalNoOfButtons; i++) {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button addTarget:self action:@selector(mybuttons:) forControlEvents:UIControlEventTouchUpInside];
    if(i==0){
    [button setTitle:[NSString stringWithFormat:@"Dashboard"] forState:UIControlStateNormal];//with title
    }
    else if(i==1){
        [button setTitle:[NSString stringWithFormat:@"Order"] forState:UIControlStateNormal];//with title
    }
    else if(i==2){
        [button setTitle:[NSString stringWithFormat:@"Product"] forState:UIControlStateNormal];//with title
    }
    else if(i==3){
        [button setTitle:[NSString stringWithFormat:@"Customers"] forState:UIControlStateNormal];//with title
    }
    else if(i==4){
        [button setTitle:[NSString stringWithFormat:@"Content"] forState:UIControlStateNormal];//with title
    }
    else if(i==5){
        [button setTitle:[NSString stringWithFormat:@"Site Analysis"] forState:UIControlStateNormal];//with title
    }
    else if(i==6){
        [button setTitle:[NSString stringWithFormat:@"Store Settings"] forState:UIControlStateNormal];//with title
    }
    else if(i==7){
        [button setTitle:[NSString stringWithFormat:@"CMS Settings"] 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 = 0;//half of the width
    //button.layer.borderColor=[UIColor clearColor];
    button.layer.backgroundColor=[UIColor blueColor].CGColor;
    button.titleLabel.font = [UIFont systemFontOfSize:10];
    button.layer.borderWidth=0.0f;
    button.tag=i;
    [menuScrollView addSubview:button];
}
menuScrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
[self.view addSubview:menuScrollView];

}

and it solves...Happy Coding... :))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文