Reputation: 7484
I want to add n number of views to a scroll view with paging enabled.
if hit the "add view" button say 5 times, all 5 get added w/out uiimageviews. but if I swipe to the 2nd view and click "add view" again, 2 UIImageViews get added to the scroll view (subviews). why are these image views being added after I swipe to the next view?
here I set up the view:
- (void)viewDidLoad
{
CGRect myRect = CGRectMake(0, 150, ([[UIScreen mainScreen] bounds].size.width), ([[UIScreen mainScreen] bounds].size.height - (64 + 150)));
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:myRect];
myScrollView.backgroundColor = [UIColor lightGrayColor];
myScrollView.pagingEnabled = TRUE;
myScrollView.showsHorizontalScrollIndicator = TRUE;
myRect = CGRectMake(20, 75, 150, 13);
myRect = CGRectMake(400, 50, 100, 50);
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(400, 50, 100, 50);
myButton.titleLabel.text = @"get views";
[myButton addTarget:self action:@selector(makePlantEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
myRect = CGRectMake(0, 0, (768/2), (1004/2));
[self.view addSubview:myScrollView];
self.mainScrollView = myScrollView;
[super viewDidLoad];
}
and here is my method for adding views:
-(IBAction)makePlantEvent:(id)sender
{
NSLog(@"number of views in scrollview: %d", [self.mainScrollView.subviews count]);
NSLog(@"views in scrollView: %@", self.mainScrollView.subviews);
if ([self.mainScrollView.subviews count] == 0)
{
NSLog(@"adding first view");
CGRect myRect = CGRectMake(10, 10, self.mainScrollView.frame.size.width - 20, self.mainScrollView.frame.size.height - 20);
UIView *myUIView = [[UIView alloc] initWithFrame:myRect];
myUIView.backgroundColor = [UIColor whiteColor];
myUIView.layer.borderWidth = 1.0f;
myUIView.layer.borderColor = [UIColor blackColor].CGColor;
self.mainScrollView.contentSize = CGSizeMake(768, mainScrollView.frame.size.height);
[self.mainScrollView addSubview:myUIView];
}
else if ([self.mainScrollView.subviews count] >= 1)
{
NSLog(@"adding %d view", ([self.mainScrollView.subviews count] + 1));
self.mainScrollView.contentSize = CGSizeMake((768 * ([self.mainScrollView.subviews count] + 1)), mainScrollView.frame.size.height);
CGRect myRect = CGRectMake((768 * [self.mainScrollView.subviews count]) + 10, 10, self.mainScrollView.frame.size.width - 20, self.mainScrollView.frame.size.height - 20);
UIView *myUIView = [[UIView alloc] initWithFrame:myRect];
myUIView.backgroundColor = [UIColor blueColor];
myUIView.layer.borderWidth = 1.0f;
myUIView.layer.borderColor = [UIColor blackColor].CGColor;
[self.mainScrollView addSubview:myUIView];
}
NSLog(@"at end number of views in scrollview: %d", [self.mainScrollView.subviews count]);
NSLog(@"at end views in scrollView: %@", self.mainScrollView.subviews);
}
my output: add 2 views
2011-11-28 13:42:39.976 Inspect[87637:207] BEGINING OF METHOD number of views in scrollview: 0
2011-11-28 13:42:39.977 Inspect[87637:207] BEGINING OF METHODviews in scrollView: (
)
2011-11-28 13:42:39.978 Inspect[87637:207] adding first view
2011-11-28 13:42:39.978 Inspect[87637:207] at end number of views in scrollview: 1
2011-11-28 13:42:39.979 Inspect[87637:207] at end views in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>"
)
2011-11-28 13:42:41.652 Inspect[87637:207] BEGINING OF METHOD number of views in scrollview: 1
2011-11-28 13:42:41.653 Inspect[87637:207] BEGINING OF METHODviews in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>"
)
2011-11-28 13:42:41.654 Inspect[87637:207] adding 2 view
2011-11-28 13:42:41.654 Inspect[87637:207] at end number of views in scrollview: 2
2011-11-28 13:42:41.655 Inspect[87637:207] at end views in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>",
"<UIView: 0x8a1f390; frame = (778 10; 748 790); layer = <CALayer: 0x8a26d70>>"
)
swipe to 2nd view and click button again:
2011-11-28 13:42:49.628 Inspect[87637:207] BEGINING OF METHOD number of views in scrollview: 4
2011-11-28 13:42:49.629 Inspect[87637:207] BEGINING OF METHODviews in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>",
"<UIView: 0x8a1f390; frame = (778 10; 748 790); layer = <CALayer: 0x8a26d70>>",
"<UIImageView: 0x108016d0; frame = (760 1; 7 808); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x10800200>>",
"<UIImageView: 0x10801690; frame = (1152 802; 383 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x108000a0>>"
)
2011-11-28 13:42:49.630 Inspect[87637:207] adding 3 view
2011-11-28 13:42:49.630 Inspect[87637:207] at end number of views in scrollview: 5
2011-11-28 13:42:49.631 Inspect[87637:207] at end views in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>",
"<UIView: 0x8a1f390; frame = (778 10; 748 790); layer = <CALayer: 0x8a26d70>>",
"<UIImageView: 0x108016d0; frame = (760 1; 7 808); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x10800200>>",
"<UIImageView: 0x10801690; frame = (1152 802; 383 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x108000a0>>",
"<UIView: 0xf4002f0; frame = (3082 10; 748 790); layer = <CALayer: 0xf4001c0>>"
so the swipe adds the two UIImageViews. Why?
Upvotes: 1
Views: 125
Reputation: 29767
There are scroll indicators. When your scrollview contentSize > scrollview size then UIScrollView automatically adds two subviews (UIImageView) to itself.
To disable this feature set showsHorizontalScrollIndicator and showsVerticalScrollIndicator properties to NO.
Upvotes: 2