PatrikD
PatrikD

Reputation: 377

Xcode UIImageView in UIScrollView IOS

can you help me? I would like add background image with scrolling, because my background image is too high. How can I do this? I add UIScrollView and into I add UIImageView but it doesnt scroll.

Thank you for replies.

Upvotes: 0

Views: 2883

Answers (2)

Ty Lertwichaiworawit
Ty Lertwichaiworawit

Reputation: 2950

Here is working code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //create scrollView and set the frame to the size you want.
    //In this example the scrollView frame is the whole ViewController size
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [scrollView setBackgroundColor:[UIColor clearColor]];

    //create a UIImage,set the imageName to your image name
    UIImage *image = [UIImage imageNamed:@"YourImageName.png"];
    //create UIImageView and set imageView size to you image height
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, image.size.height)];
    [imageView setImage:image];

    //add ImageView to your scrollView
    [scrollView addSubview:imageView];

    //set content size of you scrollView to the imageView height
    [scrollView setContentSize:CGSizeMake(self.view.frame.size.width, imageView.frame.size.height)];


    [self.view addSubview:scrollView];
}

Upvotes: 2

Enrico Susatyo
Enrico Susatyo

Reputation: 19790

If you want the scroll view to scroll, set the content size to be bigger than the scroll view frame.

Upvotes: 1

Related Questions