我希望在启用分页的滚动视图中添加n个视图。
如果点击“添加视图”按钮说5次,则所有5次添加w / out uiimageviews。但如果我再次滑动到第二个视图并再次单击“添加视图”,则会将2个UIImageViews添加到滚动视图(子视图)。为什么在我滑动到下一个视图后添加这些图像视图?
这里我设置了视图:
- (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];
}
这是我添加视图的方法:
-(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);
}
我的输出: 添加2个视图
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>>"
)
滑动到第二个视图并再次单击按钮:
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>>"
所以滑动添加了两个UIImageViews。为什么呢?
答案 0 :(得分:2)
有滚动指示器。当您的scrollview contentSize&gt; scrollview大小然后UIScrollView自动添加两个子视图(UIImageView)给自己。
要禁用此功能,请将showsHorizontalScrollIndicator和showsVerticalScrollIndicator属性设置为NO。