【问题标题】:doesn't work correctly. addSubview problem不能正常工作。 addSubview 问题
【发布时间】:2010-06-03 11:18:38
【问题描述】:

帮帮我。 addSubview 不起作用。 我想在 scrollView 上添加“ContentView”。 但是“ContentView”没有出现在屏幕上。 “ContentView”是 UIView。

当我从 [self.view addSubview:scrollView] 更改为 self.view=scrollView 时, addSubview 不起作用。

请教我如何在这个屏幕上添加 UIView!!

- (void)viewDidLoad {
[super viewDidLoad];

scrollViewMode = ScrollViewModeNotInitialized;
mode = 1;
imageViewArray = [[NSMutableArray alloc] init]; 
mainStatic = [[MainStatics alloc] init];    
[mainStatic setSetting];
NSString* path = [[NSBundle mainBundle] pathForResource:@"Files" ofType:@"plist"];      
NSArray* dataFiles = [NSArray arrayWithContentsOfFile:path];    
kNumImages = [dataFiles count]; 

CGRect frame = [UIScreen mainScreen].applicationFrame;
scrollView = [[touchClass alloc] initWithFrame:frame];
scrollView.delegate = self;
scrollView.maximumZoomScale = 5.0f;
scrollView.minimumZoomScale = 1.0f;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setDelegate:self];
scrollView.delaysContentTouches=NO;
scrollView.userInteractionEnabled = YES;
[scrollView setCanCancelContentTouches:NO];

NSUInteger i;                                                   
for (i=0;i<[dataFiles count];i++)
{
    UIImage* image = [UIImage imageNamed:[dataFiles objectAtIndex:i]];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.contentMode = UIViewContentModeScaleAspectFit;                
    imageView.userInteractionEnabled = NO;                                  
    CGRect rect = imageView.frame;                                          
    rect.size.height = 480;                                 
    rect.size.width = 320;                                      
    imageView.frame = rect;                                                 
    imageView.tag = i+1;                                                                       
            [imageViewArray addObject:imageView];
}

self.view = scrollView;

[self setPagingMode];
UIView* contentView;                                            
CGRect scRect = CGRectMake(0, 436, 320, 44);                    
contentView = [[UIView alloc] initWithFrame:scRect];
[contentView addSubview:toolView];                              
[self addSubview:contentView];                              

}

【问题讨论】:

    标签: iphone uiview scrollview addsubview


    【解决方案1】:

    我在以下函数中使用了图像数组。 toolView 是带有 UIToolbar 的 UIView。 所以我想在屏幕上添加工具栏。 是的,touchClass 是 UIScrollView 的子类。 1.我明白了。我忘记发布了。谢谢。 2.好的。我修改了这个。但是“ContentView”没有出现。

    还有其他原因吗?

    - (void)setPagingMode {
    
        CGSize pageSize = [self pageSize];
        NSUInteger page = 0;
        for (UIView *view in imageViewArray){
            [scrollView addSubview:view];
            view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
        }
    
        scrollView.pagingEnabled = YES;
        scrollView.showsVerticalScrollIndicator = scrollView.showsHorizontalScrollIndicator = YES;
        scrollView.contentSize = CGSizeMake(pageSize.width * [imageViewArray count], pageSize.height);
        scrollView.contentOffset = CGPointMake(pageSize.width * currentPage, 0);    
        scrollViewMode = ScrollViewModePaging;
    }
    

    【讨论】:

      【解决方案2】:

      来源不清楚。 你永远不会在 UIImageView 中使用数组。没有什么是 toolView 等信息... 请提供更详细的来源。 touchClassUIScrollView 的子类吗?

      我在您的代码中注意到的:

      1. 您不释放imageimageView。所有这些图像 imageViews 都将被泄露。在循环结束时,您应该添加 [imageView release];[image release];
      2. 我不认为发送[self addSubview:contentView];是个好主意尝试使用[self.view addSubview:contentView];

      【讨论】:

      • 谢谢。请查看我的新评论。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 2013-03-30
      • 2010-11-30
      • 2021-10-02
      • 2017-08-06
      • 2013-11-22
      相关资源
      最近更新 更多