【问题标题】:how to add multiple buttons on scrollview如何在滚动视图上添加多个按钮
【发布时间】:2011-12-23 02:42:47
【问题描述】:

我想与滚动视图一起动态添加按钮,假设在滚动视图上要添加大约 100 个按钮,将其添加到 nib 文件中会很麻烦,所以我想知道如何编写代码,动态添加按钮在滚动视图的图像视图之上

【问题讨论】:

    标签: ios iphone objective-c uiscrollview uibutton


    【解决方案1】:

    你需要做的是创建一个循环,创建UIButtons。设置按钮并将它们作为子视图添加到UIScrollView。代码如下。

    NSUInteger i;
    int xCoord=0;
    int yCoord=0;
    int buttonWidth=100;
    int buttonHeight=50;
    int buffer = 10;
    for (i = 1; i <= 100; i++)
    {
        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        aButton.frame     = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
        [aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:aButton];
    
        yCoord += buttonHeight + buffer;
    }
    [scrollView setContentSize:CGSizeMake(700, yCoord)];
    

    我在这里所做的基本上是为 X 和 Y 坐标设置变量。当我循环创建UIButtons 时,我正在创建适当的CGRect 结构来决定在UIScrollView 中放置按钮的位置。将该按钮添加到 scrollView 后,将 X 和 Y 值更改为您想要放置下一个按钮的位置。

    最后不要忘记为滚动视图设置ContentSize,以便它启用滚动。

    PS:所有这些代码都是徒手输入的,可能有小的语法错误,但逻辑是可靠的。

    【讨论】:

    • 嗨,它工作得很好 btw buttonwidth n buttonheight 以像素为单位对吗?
    • 其实你不需要担心这个。 iOS 负责将像素标准化为 X、Y 坐标。所以基本上你可以在 iPad 或 iPhone 上运行相同的代码而不会遇到麻烦。
    • 再问一个问题,因为我创建了一些按钮,我需要在代码中添加位于 xml url 中的图像
    • link 。这是该问题的链接
    【解决方案2】:

    您可以使用以下代码在滚动视图上添加多个按钮:

    第 1 步:

    Delegate "UIScrollViewDelegate" to your Viewcontroller.h
    
    for example:
     @interface Viewcontroller : UIViewController<UIScrollViewDelegate>
    

    第 2 步:

    //create you UIScrollView
    UIScrollView  *Scroll_View= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,self.view.frame.size.width ,self.view.frame.size.height-0)];
    Scroll_View.delegate= self;
    self.automaticallyAdjustsScrollViewInsets= NO;
    Scroll_View.backgroundColor= [UIColor clearColor];
    Scroll_View.scrollEnabled= YES;
    Scroll_View.userInteractionEnabled= YES;
    [Scroll_View setShowsHorizontalScrollIndicator:NO];
    [Scroll_View setShowsVerticalScrollIndicator:YES];
    [self.view addSubview:Scroll_View];
    

    第 3 步:

    NSArray  *optionsAry= [NSArray arrayWithObjects:@"My Profile & My Orders" ,@"Track Order" ,@"Settings" ,@"Contact Us" ,@"About Us" ,@"Terms & Conditions" ,@"Currency Converter" ,@"System Info" ,@"e-Commerce News App (News Billa)" ,@"Social Media" ,nil];
    
    int y= 20;
    for(int i=0; i<optionsAry.count; i++){
        UIButton  *BTN_For_More= [[UIButton alloc] initWithFrame:CGRectMake(20 ,y , Scroll_View.frame.size.width-40 ,35)];
        BTN_For_More.tag= i;
        BTN_For_More.backgroundColor= [UIColor whiteColor];
        [BTN_For_More addTarget:self action:@selector(BTN_For_More_Action:) forControlEvents:UIControlEventTouchUpInside];
        BTN_For_More.titleLabel.text= [NSString stringWithFormat:@"%@",[optionsAry objectAtIndex:i]];
        BTN_For_More.titleLabel.textColor= [UIColor colorWithRed:12/255.0 green:104/255.0 blue:172/255.0 alpha:1.0f];
        BTN_For_More.titleLabel.textAlignment= NSTextAlignmentCenter;
        BTN_For_More.titleLabel.font= [UIFont fontWithName:@"SourceSansPro-Regular" size:15];
        //3D effects Start
        BTN_For_More.layer.shadowColor= [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5f].CGColor;
        BTN_For_More.layer.shadowOffset= CGSizeMake(0 ,0);//2,2
        BTN_For_More.layer.shadowRadius= 2.0;
        BTN_For_More.layer.shadowOpacity= 1.0;
        BTN_For_More.layer.masksToBounds= NO;
        //3D effects End
    
    
        [Scroll_View addSubview:BTN_For_More];
    
        y= BTN_For_More.frame.size.height + y + 20;
    }//for loop END //10
    
    Scroll_View.contentSize= CGSizeMake(Scroll_View.frame.size.width ,y);
    

    第 4 步:

    -(void) BTN_For_More_Action:(NSString *)myString{
       NSLog(@"you clicked on button %@", myString);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2012-06-08
      相关资源
      最近更新 更多