Ivan Aguilar
Ivan Aguilar

Reputation: 33

Creating UIScrollView Programmatically

I am trying to create a UIScrollView programatically. I set a property and synthesize it.

@property (weak, nonatomic) IBOutlet UIScrollView *topScrollView;
@synthesize topScrollView;

I then have a method that does this.

[topScrollView setFrame:CGRectMake(320, 0, 320, 65)];
[topScrollView setContentSize:CGSizeMake(500, 100)];
[topScrollView setBackgroundColor:[UIColor greenColor]];
[topScrollView setScrollEnabled:YES];
[topScrollView setShowsHorizontalScrollIndicator:YES];
[topScrollView setShowsVerticalScrollIndicator:NO];
[[self view] addSubview:topScrollView]; 

I put this in viewDidLoad. This does not create the scroll view. I think it is because the scroll view has not been initialized. I can do the allocation and initialization in the above method but then when I want to use it in another method it wont work. I looked at Programmatically added UIScrollView used in multiple methods but did not help too much. There is probably a simple solution that I am not aware of. What can I do to programmatically create this scroll view and be able to use it in another method(mainly to conduct animations with it).

Thanks,

Ivan

Upvotes: 3

Views: 9404

Answers (4)

Wissa
Wissa

Reputation: 1592

The straightforward programmatically way in SWIFT

To wrap it up

Create a UIScrollView

let scrollView = UIScrollView() 
scrollView.translatesAutoresizingMaskIntoConstraints = false

Use a Single Child View to Hold All of Your Content Subviews

let contentView = UIView()
contentView..translatesAutoresizingMaskIntoConstraints = false

Usually, you only want your content to scroll in one direction. In most cases to scroll vertically. Therefore set the width of the content view to be the width of the scroll view.

NSLayoutConstraint.activate([
    contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor)

Attach four constraints (top, bottom, left, right) from our single content view to the scroll view.

    contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
    contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
    contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
    contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),

Make sure you have constraints attached to all four sides of the content view so that it will expand to the size of your content.

// After Adding your subviews to the contentView make sure you've those two constraints set:

    firstSubView.topAnchor.constraint(equalTo: contentView.topAnchor),
    .
    .
    .
    lastSubView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
])

Reference: Using UIScrollView with Auto Layout in iOS

Upvotes: 0

Kupendiran iOS
Kupendiran iOS

Reputation: 217

You can use below code to add UIScrollView on yourView :-

Step 1:

Delegate "UIScrollViewDelegate" to your ViewController.h

for example:
  @interface yourViewController:UIViewController<UIScrollViewDelegate> 

Step 2:

//create you UIScrollView
UIScrollView  *MyScrollVw= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,320 ,480)]; 
MyScrollVw.delegate= self;
[MyScrollVw setShowsHorizontalScrollIndicator:NO];
[MyScrollVw setShowsVerticalScrollIndicator:YES];
MyScrollVw.scrollEnabled= YES;
MyScrollVw.userInteractionEnabled= YES;
[yourView addSubview:MyScrollVw];
MyScrollVw.contentSize= CGSizeMake(320 ,1500);//(width,height)

Step 3:

you want to implement the scrollView Delegates in ViewController.m

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
   return imgView;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
   NSLog(@"Did end decelerating");
   //do your code here
}   
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
   NSLog(@"Did scroll");
   //do your code here
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"Did end dragging");
   //do your code here
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"Did begin decelerating");
   //do your code here
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
   NSLog(@"Did begin dragging");
   //do your code here
}

Upvotes: 0

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Simple Method: You can created multiple times if you need means

- (void)scrollView
{

    int x = 0;
    int y = 10;
    for(int i=0; i<5; i++)
    {
        UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
        scrollview.showsVerticalScrollIndicator=YES;
        scrollview.scrollEnabled=YES;
        scrollview.userInteractionEnabled=YES;
        scrollview.backgroundColor = [UIColor greenColor];
        [self.view addSubview:scrollview];
        //scrollview.contentSize = CGSizeMake(50,50);
        x=x+55;
    }
}

Upvotes: 0

Caleb
Caleb

Reputation: 124997

I think it is because the scroll view has not been initialized.

Right.

I can do the allocation and initialization in the above method but then when I want to use it in another method it wont work.

It will if you assign your newly minted scroll view to a property or instance variable. That's what they're for.

Oh, one other thing. You'll need to make sure that your scroll view is retained somehow. That means changing your topScrollView property to strong, or adding it to a view (which will then retain it), or both.

Upvotes: 2

Related Questions