CAN
CAN

Reputation: 1727

ScrollView image addSubview issue

I'm trying to add an imageView in the ScrollView but I always get the ScrollView empty. Please where would be my issue? While I have added the delegate method <UIScrollViewDelegate> also made the connection in the storyboard.

My code:

- (void) viewDidLoad {    
    [super viewDidLoad];
    [self slider];
}

-(void) slider {

    _scrlView = [[UIScrollView alloc]init];    
    _scrlView.backgroundColor = [UIColor blackColor];
    _scrlView.pagingEnabled = YES;
    _scrlView.delegate = self;
    _scrlView.showsHorizontalScrollIndicator = false;
    _scrlView.layer.cornerRadius = 2;
    _scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);

    _img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
    _img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
    _img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];

    _img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1234.jpg"]];
    _img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1234.jpg"]];
    _img3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1234.jpg"]];

    [_scrlView addSubview:_img1];
    [_scrlView addSubview:_img2];
    [_scrlView addSubview:_img3];
}

Upvotes: 0

Views: 200

Answers (5)

iGautam
iGautam

Reputation: 121

My reply would be

  • if you are not using Storyboard or Interface builder , also autolayout is unchecked, set the frame of uiscrollview after its allocation and use alloc init only one time for one image. Add image on scrollview and scrollview on superview

  • if you are not using Storyboard or Interface builder and autolayout is checked, then put proper constraints on scrollview and imageviews. Read this

https://developer.apple.com/library/ios/technotes/tn2154/_index.html

Upvotes: 0

Nirav Gadhiya
Nirav Gadhiya

Reputation: 6342

You are reallocating the imageviews, try to set image after allocating it once.

Another thing : Dont init scrollview again.

- (void)viewDidLoad {

[super viewDidLoad];

[self slider];
}

-(void)slider{

_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = YES;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = false;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);

_img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];

[_img1 setImage:[UIImage imageNamed:@"1234.jpg"]];
[_img2 setImage:[UIImage imageNamed:@"1234.jpg"]];
[_img3 setImage:[UIImage imageNamed:@"1234.jpg"]];

[_scrlView addSubview:_img1];
[_scrlView addSubview:_img2];
[_scrlView addSubview:_img3];
}

Upvotes: 0

dasdom
dasdom

Reputation: 14073

First, you don't add the scrollView to any view, so it won't be presented on screen.

Second, this:

_scrlView = [[UIScrollView alloc]init];

creates a scrollView with CGRectZero which is the same like writing:

_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,0,0)];

This means the contentSize is also CGRectZero.

Edit: If you don't want to override the creation of the scrollView (because you have already set it in Interface Builder), remove the line _scrlView = [[UIScrollView alloc]init];. But if you are using Auto Layout in the storyboard you should call [self slider] in viewDidLayoutSubviews.

Upvotes: 2

Kaey
Kaey

Reputation: 4645

You need to allocate the ScrollView if have not used a storyboard. Also in that case, specify a frame:

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

Upvotes: 0

Viral Savaj
Viral Savaj

Reputation: 3389

You need to set frame first, for getting its width and height,

- (void)viewDidLoad {

    [super viewDidLoad];

    [self slider];
}

-(void)slider{
   _scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(YOUR_X, YOUR_X, YOUR_WIDTH, YOUR_HIEGHT)]; 
   // do this if you have not added Scrollview in storyboard, if Added, then no need to invoke, init by line [[UIScrollView alloc] init] ;
    ...
    ...
   [self.view addSubview:_scrlView];
}

I would suggest one more thing, to call your slider method from viewDidAppear method of UIView

Enjoy Coding !!

Upvotes: 0

Related Questions