问题
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
回答1:
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.
回答2:
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;
}
}
回答3:
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
}
来源:https://stackoverflow.com/questions/17223707/creating-uiscrollview-programmatically