2

我有一个应用程序,我在其中根据我从数组创建的按钮数量动态设置滚动视图的内容大小。像这样

for(int i =0;i<[sarray count];i++) 

    {

        NSMutableDictionary *dicttable=[sarray objectAtIndex:i];

        NSString *head=[dicttable objectForKey:@"cat"];
        btn= [UIButton buttonWithType:UIButtonTypeCustom];
        int j=i+1;
        btn.frame = CGRectMake((j-1)*87,0,87, 44);
        [btn setBackgroundImage:[UIImage imageNamed:@"bar.png"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"bar_hvr.png"] forState:UIControlStateSelected];

        btn.backgroundColor = [UIColor clearColor];

        btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];

        btn.titleLabel.textColor = [UIColor whiteColor];
       [btn setTitle:head forState:UIControlStateNormal];
        btn.tag = i;
        [btn setShowsTouchWhenHighlighted:YES];

            [Scroller addSubview:btn];
                [btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

        if(btn.tag==0)
          {
            sendActionsForControlEvents:UIControlEventTouchUpInside];

           }
       else
          {
               btn.selected=NO;



          }


    }

    }


        [Scroller setContentSize:CGSizeMake([sarray count]*85, 44)];

`但这里的问题是在最后一个按钮之后滚动视图也滚动。它不需要发生,我需要滚动以视图上的最后一个按钮结束,有人可以帮我吗

4

3 回答 3

1

将滚动视图反弹设置为 NO

scrollView.bounces = NO;

是的,正如你所说,它失去了光滑度。但别无选择。如果您发现任何溶胶,请更新答案。

于 2013-08-07T12:27:07.663 回答
0

嘿,我正在向您发送完整的示例,以使按钮动态化并根据计数设置内容大小滚动视图,我刚刚给了您一个示例代码,您必须按照您的要求设置按钮框架的大小

    UIScrollView *scroll_View=[[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:scroll_View];


int y=0;

for(int i =1;i<10;i++)

{

   // NSMutableDictionary *dicttable=[sarray objectAtIndex:i];

   // NSString *head=[dicttable objectForKey:@"cat"];
   UIButton* btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(y,89,100, 50);
    [btn setBackgroundImage:[UIImage imageNamed:@"bar.png"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"bar_hvr.png"] forState:UIControlStateSelected];

    btn.backgroundColor = [UIColor clearColor];

    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    NSString *str=[NSString stringWithFormat:@"%d",i];
    btn.titleLabel.textColor = [UIColor whiteColor];
    [btn setTitle:str forState:UIControlStateNormal];
    btn.tag = i;
    [btn setShowsTouchWhenHighlighted:YES];

    [scroll_View addSubview:btn];
    [btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    y=110*i;

}




   [scroll_View setContentSize:CGSizeMake(y, 400)];

享受编码

于 2013-08-07T11:55:05.643 回答
-1

您只需声明一个类型的 yMargin 变量CGFloat并将其初始化为一些所需的值为 0。现在,每当您垂直添加新的子视图时y+=lbl.frame.size.height;

现在setContentSize:CGSizeMake([sarray count]*85, y margin+someRequiredMargin)];

(someRequiredMargin - 你想在下面添加)

于 2013-08-07T11:20:04.220 回答