无论如何要在导航栏中添加相同的滚动菜单栏吗?

人气:1,038 发布:2022-10-16 标签: ios uiviewcontroller uibutton uitabbaritem

问题描述

我已经再次发布了我的这个问题,但我没有得到完美的答案.在这里我将再次解释我的问题,这对我来说非常重要,所以我必须不惜一切代价解决它.现在我的问题是……

I have already posted my this problem another time but i have not got my answer perfectly.Here i am going to explain my problem another time, it is very important for me so at any cost i have to solve it.Now my problem is...

假设,我在 tabbarController 中有 4 个 tabbaritem 和项目 "dashboard","order","product","customer".

Suppose, I have 4 tabbaritem in a tabbarController and items "dashboard","order","product","customer".

这些标签栏的每一项都是一个调用相应的uiviewcontroller.

every item of these tabbar is a calling there respective uiviewcontroller.

dashboar 调用DashboarViewController";

dashboar calling "DashboarViewController";

订单调用orderViewController";

order calling "orderViewController";

产品调用ProductViewController";

product calling "ProductViewController";

客户调用CustomerViewController";

customer calling "CustomerViewController";

现在,我必须在每个 uiviewcontroller 上设置一个滚动菜单栏,并且这个菜单栏包含 4 个按钮.这些按钮名称与标签栏项目名称仪表板"、订单"、产品"、客户"相同.

Now, i have to set a scrolling menubar at every uiviewcontroller and this menu bar containing 4 buttons. These button names are same as tabbar items name "dashboard","order","product","customer".

现在,当我按下菜单栏的按钮时,相应的控制器将显示与标签栏项目相同的显示.假设我正在按订单"标签栏项目,那么它将显示订单视图控制器".当我看到这个视图控制器时,它还会显示视图控制器顶部的菜单栏.现在,如果我点击这个orderviewcontroller"中的product"按钮,那么它会发回给我productViewcontroller".

Now when i press the button of the menu bar then respective controller will show same as for tabbar items. suppose i am pressing "order" tabbar item then it will show the "orderviewcontroller". when i will see this view controller it will also show me that menu bar at the top of the viewcontroller.Now, if i am click "product" button in this "orderviewcontroller" then it will sent back to me "productViewcontroller".

这意味着标签栏项目和滚动菜单栏的按钮将相同.

thats mean tabbar item and button of the scroll menubar will work same.

现在我已经完成了这些,我以前的帖子图片如何在多个视图控制器中制作相同的按钮?

still now i have done these, my previous post image How can i make same button in multiple view controller?

如果有人知道怎么做,请一步一步解释.我不需要你提供任何代码.阅读我之前的帖子后,一步一步解释我该怎么做

If some know how can do that then please explain it step by step.I do not need to give any code from you.just explain it step by step how can i do that after reading my previous post

提前致谢.

推荐答案

哈哈哈.....当我解决它的时候真是太有趣了.不管我用什么不同的方式解决了这个问题,我没有使用滚动视图按钮仅用于控制器的控制器,在每个控制器中,我已经对滚动视图中的按钮创建位置以及按钮的操作进行了更改,我刚刚更改了选项卡栏控制器的选定索引.

Ha ha ha .....it was so much fun when i solved it.whatever i solved this problem in different way, i did not use scrollview button controller for controller just, in every controller i have made function to where buttons within a scrollview create and on action of button i have just change the selected index of the tabbar controller.

-(void)viewDidload 我写了这段代码

     UIView *scrollViewBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
scrollViewBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"topmenu_bg.png"]];

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

[self createMenuWithButtonSize:CGSizeMake(92.0, 30.0) withOffset:5.0f noOfButtons:7];   

这里是按钮的创建和操作

here is the button create and action

-(void)mybuttons:(id)sender{    
NSLog(@"mybuttons called");
UIButton *button=(UIButton *)sender;
NSLog(@"button clicked is : %iBut 

",button.tag);
int m = button.tag;
for(int j=0;j<8;j++){
    if(button.tag == m){
        self.tabBarController.selectedIndex = m;
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_hover.png"] forState:UIControlStateHighlighted]; //sets the background Image]            
    }
    if(button.tag != m){
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
    }
}
}       

 -(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] retain];
    [button addTarget:self action:@selector(mybuttons:) forControlEvents:UIControlEventTouchUpInside];
    (button).titleLabel.font =  [UIFont fontWithName:@"Arial" size:12];
    if(i==0){
        [button setTitle:[NSString stringWithFormat:@"Dashboard"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_hover.png"] forState:UIControlStateNormal]; //sets the background Image]
    }
    if(i==1){
        [button setTitle:[NSString stringWithFormat:@"Order"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    }
    if(i==2){
        [button setTitle:[NSString stringWithFormat:@"Product"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    }
    if(i==3){
        [button setTitle:[NSString stringWithFormat:@"Customers"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    }
    if(i==4){
        [button setTitle:[NSString stringWithFormat:@"Content"] forState:UIControlStateNormal];//with title
    }
    if(i==5){
        [button setTitle:[NSString stringWithFormat:@"Site Analysis"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    }
    if(i==6){
        [button setTitle:[NSString stringWithFormat:@"Store Settings"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    }
    if(i==7){
        [button setTitle:[NSString stringWithFormat:@"CMS Settings"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    }
    button.frame = CGRectMake(i*(offset+buttonSize.width), 6.0, buttonSize.width, buttonSize.height);
    button.clipsToBounds = YES;
    button.showsTouchWhenHighlighted=YES;
    button.layer.cornerRadius = 5;//half of the width
    button.layer.borderColor=[UIColor clearColor].CGColor;
    button.layer.borderWidth=0.0f;
    button.tag=i;
    [menuScrollView addSubview:button];
}
menuScrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
[self.view addSubview:menuScrollView];

}

899