无法正常工作。添加子视图问题

发布于 2024-09-04 14:58:09 字数 2266 浏览 12 评论 0原文

帮我。 addSubview 不起作用。 我想在滚动视图上添加“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];                              

}

Help me.
addSubview doesn't work.
I want to add "ContentView" on scrollView.
But "ContentView" doesn't appear on screen.
"ContentView" is UIView.

When I change to self.view=scrollView from [self.view addSubview:scrollView],
addSubview doesn't work.

Please teach me how to add UIView on this screen!!

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萝莉病 2024-09-11 14:58:09

我在以下函数中使用了图像数组。
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;
}

I used image array in following function.
toolView is UIView with UIToolbar.
So I want to add toolbar to screen.
Yes, touchClass is subclass of UIScrollView.
1.I see.I forgot release this.Thank you.
2.OK. I modified this.But "ContentView" didn't apear.

are there another reason?

- (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;
}
浅听莫相离 2024-09-11 14:58:09

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

我在您的代码中注意到:

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

Source isn't clear.
You never use array with UIImageView. There is no info what is toolView etc...
Please provide more detailed source. Is touchClass a subclass of UIScrollView?

What I noticed in your code:

  1. You don't release image and imageView. All this images imageViews will be leaked. At the end of loop you should add [imageView release]; and [image release];
  2. I don't think that it is good idea to send [self addSubview:contentView]; Try to use [self.view addSubview:contentView];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文