scrollview不在iphone中滚动

时间:2013-06-21 11:10:52

标签: ios objective-c uiscrollview

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

我的代码如下

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

5 个答案:

答案 0 :(得分:2)

在Viewdidload中添加此内容

self.scroll_main.contentSize = CGSizeMake(320,self.view_main.frame.size.height+57);

答案 1 :(得分: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];

我希望它会帮助你......

答案 2 :(得分:1)

试试这个

- (void)viewDidLoad {
    [super viewDidLoad]; 
    [scrollView setScrollEnabled:YES];
    [scrollview setFrame:self.view.bounds];
    [scrollview setContentSize:CGSizeMake(1500,50)];
}

答案 3 :(得分: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

答案 4 :(得分:0)

我无法计算有关滚动视图的教程数量。但是你肯定需要设置滚动视图的contentSize以便它可以滚动。 [yourScrollView setContentSize:some CGSize];

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