以编程方式UIScrollView无法正常工作

时间:2014-07-15 05:15:39

标签: ios objective-c uiscrollview uitextfield

我是IOS的新手。我尝试将8 UItext-field添加到UIScrollView。 我的代码是这样的: -

 City=[[UITextField alloc]init];
    City.frame=CGRectMake(45, 150, 221, 30);
    [self.view addSubview:City];
    City.backgroundColor=[UIColor whiteColor];
    City.placeholder=@"Email";
    [City setFont:[UIFont fontWithName:@"Times New Roman" size:20]];
    //[Username setTextAlignment:UITextBor];
    City.layer.borderColor=[[UIColor yellowColor]CGColor];
    City.layer.borderWidth=2.0;
    [City setBorderStyle:UITextBorderStyleLine];
same as 
city1=[[UITextField alloc]init];
City1.frame=CGRectMake(45, 195, 221, 30);

  |
  |
  |
  |
city8=[[UITextField alloc]init];
city8.frame=CGRectMake(45, 355, 221, 30);

Jest我创建8textFileds现在所有都添加到UIScrollview。

UIScrollView *scroll = [UIScrollView alloc];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(45, 105, 221, 355)];

    scroll.pagingEnabled = YES;

    scroll.showsHorizontalScrollIndicator = YES;
    scroll.showsVerticalScrollIndicator = YES;
    scroll.scrollsToTop = NO;
    scroll.delegate = self;             //say "NO" to disable scroll
    [self.view addSubview:scroll];
    [scroll addSubview:City];
    [scroll addSubview:City1];
     |
     |
    [scroll addSubview:City8];

我只需要UITextField和UIScrollView.First创建UITextField,然后创建ScrollView并添加两者,但我的textFileds不添加Scroll

请给我任何想法,请告诉我我的代码中有什么问题

5 个答案:

答案 0 :(得分:1)

删除此代码并检查

[self.view addSubview:City];

在scrollview中添加所有文本字段后放置此代码。

[self.view addSubview:scroll];

它可能对你有帮助。

答案 1 :(得分:1)

试试这段代码

 UIScrollView *scroll ;
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //set your frame according to your viewframe or text field frame.here give width and height of scrolling
    scroll.contentSize=CGSizeMake(self.view.frame.size.width+100,self.view.frame.size.height+100);
        scroll.pagingEnabled = YES;
       self.scrView.showsHorizontalScrollIndicator = YES;
        self.scrView.showsVerticalScrollIndicator=YES;

答案 2 :(得分:1)

 UIScrollView *scroll = [UIScrollView alloc];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    scroll.pagingEnabled = YES;
    scroll.showsHorizontalScrollIndicator = YES;
    scroll.showsVerticalScrollIndicator = YES;
    scroll.scrollsToTop = NO;
    scroll.delegate = self;             //say "NO" to disable scroll
    scroll.contentSize = CGSizeMake(scrollView.frame.size.width*8,0);

 UITextField*City=[[UITextField alloc]init];
    City.frame=CGRectMake(45, 150, 221, 30);
    City.backgroundColor=[UIColor whiteColor];
    City.placeholder=@"Email";
    [City setFont:[UIFont fontWithName:@"Times New Roman" size:20]];
    //[Username setTextAlignment:UITextBor];
    City.layer.borderColor=[[UIColor yellowColor]CGColor];
    City.layer.borderWidth=2.0;
    [City setBorderStyle:UITextBorderStyleLine];

UITextField*city1=[[UITextField alloc]init];
    city1.frame=CGRectMake(45, 195, 221, 30);
    city1.backgroundColor=[UIColor whiteColor];
    city1.placeholder=@"Email1";
    UITextField*city8=[[UITextField alloc]init];
    city8.frame=CGRectMake(45, 355, 221, 30);
    [scroll addSubview:City];
    [scroll addSubview:city1];
    [scroll addSubview:city8];
   [self.view addSubview:scroll];

希望它有所帮助。

答案 3 :(得分:1)

如果您想将UITextField's添加到UIScrollView而不需要添加self.view。 因此,请删除将UITextField's添加到self.view的语句。虽然这不是你问的问题,但是没有必要在视图中添加这些文本字段。

所以全部删除 - > [self.view addSubview:City];

就你的问题而言,你做的一切都是正确的,除了一件事,那就是"增加内容大小"您的UIScrollView。您可以按如下方式增加

scroll.contentSize = CGSizeMake(Width,height); 

宽度和宽度您可以根据自己的要求设置高度。

了解更多信息,请参阅UIScrollView reference

答案 4 :(得分:0)

City=[[UITextField alloc]init];
   City.frame=CGRectMake(45, 150, 221, 30);
    [self.view addSubview:City];//----------------> Please remove this line
    City.backgroundColor=[UIColor whiteColor];
    City.placeholder=@"Email";
    [City setFont:[UIFont fontWithName:@"Times New Roman" size:20]];
    //[Username setTextAlignment:UITextBor];
    City.layer.borderColor=[[UIColor yellowColor]CGColor];
    City.layer.borderWidth=2.0;
    [City setBorderStyle:UITextBorderStyleLine];
same as 
city1=[[UITextField alloc]init];
City1.frame=CGRectMake(45, 195, 221, 30);

  |
  |
  |
  |
city8=[[UITextField alloc]init];
city8.frame=CGRectMake(45, 355, 221, 30);

创建所有文本字段(八个文本字段)后,创建ScrollView

//UIScrollView *scroll = [UIScrollView alloc]; //---------------> No Need this line
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(45, 105, 221, 355)];

    scroll.pagingEnabled = YES; //---------------->>> Do you need paging ? Other wise set NO

    scroll.showsHorizontalScrollIndicator = YES;
    scroll.showsVerticalScrollIndicator = YES;
    scroll.scrollsToTop = NO;
    scroll.delegate = self;             //say "NO" to disable scroll
    [scroll addSubview:City];
    [scroll addSubview:City1];
     |
     |
    [scroll addSubview:City8];

将所有文本字段添加为滚动视图的子视图后,我们必须将滚动视图放置到您的父视图所以请添加以下代码

[self.view addSubview:scroll];

现在一切正常,但是现在你的滚动视图仍然无法滚动,因为我们没有设置滚动内容大小,为了做到这一点

1)您的文本字段为高度30,两个文本字段之间的空间为15,因此总共45     你有8个文本字段Toatl = 360

2)您的起始文本字段Y轴是150而不是零,所以我们需要添加360 + 150 = 510

3)您的滚动视图宽度和文本字段宽度是常见的= 221

scroll.contentSize = CGSizeMake(510,221);

现在构建它,一切顺利,保持编码:)

相关问题