【问题标题】:scrollview is not scrolling in iphone [duplicate]滚动视图不在iphone中滚动[重复]
【发布时间】:2013-06-21 11:10:51
【问题描述】:

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

我的代码如下

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

【问题讨论】:

标签: ios objective-c uiscrollview


【解决方案1】:

在 Viewdidload 中添加这个

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

【讨论】:

    【解决方案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];
    

    希望对你有帮助..

    【讨论】:

      【解决方案3】:

      试试这个

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

      【讨论】:

        【解决方案4】:
        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
        

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-08-20
            • 2011-07-16
            • 2013-03-14
            • 1970-01-01
            • 1970-01-01
            • 2015-05-15
            相关资源
            最近更新 更多