0

我是 iphone 开发的新手。我在我的应用程序中添加了滚动视图,并在滚动视图中以图形方式添加了 10 个按钮。但是当我运行滚动视图时没有滚动

我的代码如下

- (void)viewDidLoad {
    [super viewDidLoad]; 
    [scrollview setContentSize:CGSizeMake(1500,50)];
}
4

5 回答 5

2

//UIScrollView

    scroll =[[UIScrollView alloc] initWithFrame:CGRectMake(0,70,320, 318)];
    targetLabel0 = [[UILabel alloc] init];
    CGSize labelSize0 = CGSizeMake(290, 9999);
CGSize theStringSize0 = [qtnstr sizeWithFont:targetLabel0.font constrainedToSize:labelSize0 lineBreakMode:targetLabel0.lineBreakMode];
    targetLabel0.frame = CGRectMake(targetLabel0.frame.origin.x+15, targetLabel0.frame.origin.y+10, theStringSize0.width, theStringSize0.height);
    targetLabel0.text = qtnstr;
    [targetLabel0 setNumberOfLines:0];
    targetLabel0.backgroundColor=[UIColor clearColor];
    [targetLabel0 sizeToFit];
    [scroll addSubview:targetLabel0];

    //UILabel1

    targetLabel = [[UILabel alloc] init];
    CGSize labelSize = CGSizeMake(240, 9999);
    CGSize theStringSize = [ans1 sizeWithFont:targetLabel.font constrainedToSize:labelSize lineBreakMode:targetLabel.lineBreakMode];
    targetLabel.frame = CGRectMake(targetLabel0.frame.origin.x+40, targetLabel0.frame.size.height+40, theStringSize.width, theStringSize.height);
    targetLabel.text = ans1;
    [targetLabel setNumberOfLines:0];
    targetLabel.backgroundColor=[UIColor clearColor];
    [targetLabel sizeToFit];
    [scroll addSubview:targetLabel];

    // UIButton1

    ans1btn = [[UIButton alloc] initWithFrame:CGRectMake(15,targetLabel.frame.origin.y-5,30,30)];
    ans1btn.backgroundColor=[UIColor redColor];
    [ans1btn setSelected:NO];
    [ans1btn setBackgroundImage:[UIImage imageNamed:@"gray.png"]
                       forState:UIControlStateNormal];
    [ans1btn setBackgroundImage:[UIImage imageNamed:@"green.png"]
                       forState:UIControlStateSelected];
    ans1btn.adjustsImageWhenHighlighted=YES;
    [ans1btn addTarget:self
                action:@selector(checkboxSelected:)
      forControlEvents:UIControlEventTouchUpInside];
    ans1btn.tag=1;
    [scroll addSubview:ans1btn];

    //UILAbel2

    targetLabel1 = [[UILabel alloc] init];
    CGSize labelSize1 = CGSizeMake(240, 9999);
CGSize theStringSize1 = [ans2 sizeWithFont:targetLabel1.font constrainedToSize:labelSize1 lineBreakMode:targetLabel1.lineBreakMode];
    targetLabel1.frame = CGRectMake(targetLabel.frame.origin.x, targetLabel0.frame.size.height+targetLabel.frame.size.height+80, theStringSize1.width, theStringSize1.height);
    targetLabel1.text = ans2;
    [targetLabel1 setNumberOfLines:0];
    targetLabel1.backgroundColor=[UIColor clearColor];
    [targetLabel1 sizeToFit];
    [scroll addSubview:targetLabel1];


    //UIButton2


    ans1btn1 = [[UIButton alloc] initWithFrame:CGRectMake(15,targetLabel0.frame.size.height+targetLabel.frame.size.height+75,30,30)];
    [ans1btn1 setSelected:NO];
    [ans1btn1 setBackgroundImage:[UIImage imageNamed:@"gray.png"]
                        forState:UIControlStateNormal];
    [ans1btn1 setBackgroundImage:[UIImage imageNamed:@"green.png"]
                        forState:UIControlStateSelected];
    ans1btn1.adjustsImageWhenHighlighted=YES;
    [ans1btn1 addTarget:self
                 action:@selector(checkboxSelected1:)
       forControlEvents:UIControlEventTouchUpInside];
    ans1btn1.tag=2;
    [scroll addSubview:ans1btn1];


  //Like this you can add any number of Labels and Buttons to Scrollview

   //Finally SetContentsize according to its content

    [scroll setContentSize:CGSizeMake(0,(targetLabel0.frame.size.height+targetLabel.frame.size.height+targetLabel1.frame.size.height+targetLabel2.frame.size.height+targetLabel3.frame.size.height)+250)];

    [self.view addSubview:scroll];

我希望它会帮助你..

于 2013-06-21T11:24:13.850 回答
2

在 Viewdidload 中添加这个

self.scroll_main.contentSize = CGSizeMake(320,self.view_main.frame.size.height+57);
于 2013-06-21T11:17:26.897 回答
1

尝试这个

- (void)viewDidLoad {
    [super viewDidLoad]; 
    [scrollView setScrollEnabled:YES];
    [scrollview setFrame:self.view.bounds];
    [scrollview setContentSize:CGSizeMake(1500,50)];
}
于 2013-06-21T11:15:58.057 回答
0
scrollview.fram = self.view.bounds;
[scrollview setContentSize:CGSizeMake(320,self.view.fram.size.height+50)]; // if you want scroll vertically 

或者

[scrollview setContentSize:CGSizeMake(self.view.fram.size.width+50,460)]; // if you want scroll Horizontally
于 2013-06-21T11:14:49.880 回答
0

我数不清有多少关于滚动视图的教程。但是你肯定需要设置滚动视图的 contentSize 以便它可以滚动。[yourScrollView setContentSize:一些 CGSize ];

PS 如果对于您的项目,滚动视图上的实体在稍后阶段增加,那么 UITableView 是最好的方法,而不是 UIScrollView。

于 2013-06-21T11:55:39.443 回答