2011-11-24 128 views
0

可能重复:
How can i add more than 10 buttons on a navigationbar in iphone application development?如何在导航栏上添加uiscrollview?

我想设置了含有八个按钮导航栏和该导航栏上的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]; 

} 
+2

这听起来像一个可怕的糟糕的导航栏。即使您设法通过了苹果的审核流程,我也强烈建议您不要这样做 - 它会混淆/惹恼您的用户。 – occulus

+0

我已编辑我的问题,请看看现在帮助我。 – Emon

回答

0

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

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我创建了我的菜单buttons这将是看起来像按钮上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]; 

} 

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