ScrollView图像addSubview问题

时间:2015-04-21 10:16:38

标签: ios objective-c uiscrollview

我试图在ScrollView中添加一个imageView,但我总是将ScrollView设为空。请问我的问题在哪里?虽然我添加了委托方法<UIScrollViewDelegate>,但也在故事板中建立了连接。

我的代码:

- (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];
}

5 个答案:

答案 0 :(得分:2)

首先,您不要将scrollView添加到任何视图,因此它不会显示在屏幕上。

第二,这:

_scrlView = [[UIScrollView alloc]init];

创建一个带有CGRectZero的scrollView,它与写作相同:

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

这意味着contentSize也是CGRectZero

编辑:如果您不想覆盖scrollView的创建(因为您已在Interface Builder中设置了它),请删除行_scrlView = [[UIScrollView alloc]init];。 但如果您在故事板中使用自动布局,则应在viewDidLayoutSubviews中致电[self slider]

答案 1 :(得分:0)

您需要先设置框架,以获取其宽度和高度,

- (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];
}

我建议再做一件事,用slider viewDidAppear方法调用UIView方法

享受编码!!

答案 2 :(得分:0)

如果没有使用故事板,则需要分配ScrollView。同样在这种情况下,指定一个框架:

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

答案 3 :(得分:0)

您正在重新分配图像视图,尝试在分配图像后设置图像。

另一件事:不要再次初始化scrollview。

- (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];
}

答案 4 :(得分:0)

我的回复是

  • 如果您没有使用Storyboard或Interface构建器,也取消选中autolayout,在分配后设置uiscrollview的框架,并对一个图像使用alloc init一次。在scrollview上添加图像,在superview上添加scrollview

  • 如果您未使用Storyboard或Interface构建器并且选中了autolayout,则在scrollview和imageview上设置适当的约束。阅读本文

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