UIView 未显示在 UIScrollView 中

Posted

技术标签:

【中文标题】UIView 未显示在 UIScrollView 中【英文标题】:UIView is not showing up in UIScrollView 【发布时间】:2012-07-24 08:39:16 【问题描述】:

我目前正在动态地将 UIImage 添加到我的 UIScrollView 对象中,并且一切都按预期工作。我现在需要添加额外的功能(从互联网加载时图像中心的 UIActivityIndi​​cator 和下方的标签)所以我想为什么不创建一个自定义视图,其中包含我需要的所有这些布局是,然后将其加载到滚动视图中。但是我遇到了一个问题,当我将它添加到滚动视图时,什么也没有出现。这是我所拥有的:

NewsViewController.m:

imageScrollView.contentSize = CGSizeMake(320*numberOfPages, 303);
pageControl.numberOfPages = numberOfPages;
dispatch_queue_t imageDownload = dispatch_queue_create("imageDownload", NULL);
__block NSData *temp;
    CustomImageView *customImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 303)];
    [imageScrollView addSubview:customImageView];
    [[customImageView activityIndicator] startAnimating];
    dispatch_async(imageDownload, ^
        temp = [[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.wlfarm.org/wp-content/uploads/2012/05/farm.jpg"]]retain];
        dispatch_async(dispatch_get_main_queue(), ^
            customImageView.imageView.image = [[UIImage alloc] initWithData:temp];
            [[customImageView activityIndicator] stopAnimating];
            [customImageView setNeedsDisplay];
            customImageView.caption.text = @"HAHAHAHAHHAHA";
            [imageScrollView setNeedsDisplay];
            [temp release];
        );
    );

    dispatch_release(imageDownload);

CustomImageView.h:

#import <UIKit/UIKit.h>

@interface CustomImageView : UIView

    IBOutlet UIImageView *imageView;
    IBOutlet UILabel *caption;
    IBOutlet UIActivityIndicatorView *activityIndicator;


@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UILabel *caption;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@end

CustomImageView.m

#import "CustomImageView.h"  
@interface CustomImageView ()

@end

@implementation CustomImageView

@synthesize caption, imageView, activityIndicator;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


 - (void)viewDidUnload

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;


- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation

    return (interfaceOrientation == UIInterfaceOrientationPortrait);


@end

我包括我的 XIB 文件的屏幕截图,以及在模拟器上运行的程序。第一张图片在滚动视图中没有显示任何内容(使用我的自定义类进行的尝试),滚动视图的第二页是通过将 UIImageView 添加到 UIScrollView 进行的尝试(有效)。谁能指出我做错了什么?我不允许将自定义视图加载到 UIScrollView 中吗?感谢您的帮助!

IB 截图 - http://i.stack.imgur.com/gz9UL.png

ios 模拟器没有带有 CustomView 截图的图像 - http://i.stack.imgur.com/zhswq.png

带有 UIImageView 截图的 iOS 模拟器图像 - http://i.stack.imgur.com/97vmU.png

【问题讨论】:

您是否在身份检查器中正确引用了此自定义视图? 是的,在我的屏幕截图中,它显示 Custom 类应该是 CustomImageView。你指的是这个吗? 是的 - 抱歉我没有检查你的截图 请分享您的 CustomImageView 类的一些相关代码。原则上,您的自定义图像视图可能定位正确且可见,但其子视图和属性 activityIndi​​cator、标题和 imageView 不存在。 CustomImageView类的.m文件中除了我合成属性外没有任何代码。我在 IB 的屏幕截图中包含了视图层次结构。 【参考方案1】:

看起来CustomImageViewUIViewController 的子类,而不是UIImageViewUIView。您不能像这样添加UIViewController 作为子视图。将其更改为子类UIView,它应该可以工作。

要从 .nib 加载 UIView,您需要像声明 UIViewController 一样声明 IBOutlet 属性。在 IB 中定义正确的自定义类并连接所有内容,包括基本视图。然后在您的自定义视图类中覆盖以下方法。

- (id)initWithFrame:(CGRect)frame 
    self = [super initWithFrame:frame];
    if (self) 
    // Initialization code.
    //
    [[NSBundle mainBundle] loadNibNamed:@"<NIB NAME HERE>" owner:self options:nil];
    [self addSubview:self.view];
    
  return self;

您似乎已将 UIViewController 子类中的方法剪切并粘贴到您的 UIView 子类中。首先删除您在 @synthesize@end 之间粘贴的所有方法。 UIView 子类需要不同的方法从 .nib 加载,因此您需要覆盖上面显示的方法并使用代码行

[[NSBundle mainBundle] loadNibNamed:@"&lt;NIB NAME HERE&gt;" owner:self options:nil];

加载笔尖。

关于连接基本视图的进一步评论,我的意思是:

创建自定义类和 nib 文件后,打开 nib 并突出显示左侧的“文件所有者”,然后在右上角,选择左侧第三个图标,看起来像身份证或其他东西。在“类”框中,添加自定义类的名称。

在您的customClass中,添加一个名为baseView的IBOutlet UIView属性,并在根级别添加一个覆盖IB中视图的UIView,将两者连接起来。然后在上面添加其他所有内容

你的代码现在看起来像这样:

CustomImageView.h

#import <UIKit/UIKit.h>

@interface CustomImageView : UIView

    IBOutlet UIView *baseView
    IBOutlet UIImageView *imageView;
    IBOutlet UILabel *caption;
    IBOutlet UIActivityIndicatorView *activityIndicator;

@property (nonatomic, retain) IBOutlet UIView *baseView;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UILabel *caption;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@end

CustomImageView.m

#import "CustomImageView.h"  
@interface CustomImageView ()

@end

@implementation CustomImageView

@synthesize baseView, caption, imageView, activityIndicator;

- (id)initWithFrame:(CGRect)frame 
    self = [super initWithFrame:frame];
    if (self) 
    // Initialization code.
    //
    [[NSBundle mainBundle] loadNibNamed:@"<NIB NAME HERE>" owner:self options:nil];
    [self addSubview:self.baseView];
    
  return self;


@end

如果你在 IB 中连接它应该可以正常工作,只要记住添加 baseView

【讨论】:

我认为UIView 可能是您最好的选择。我们可以确定看到 CustomImageView.h 吗? CustomImageView.h 在上面给出。我继承了 UIView。 所以我需要重写 initWithFrame 并在其中加载 nib 文件?包括我的基本观点是什么意思。对不起,我是新来的,你能说得更具体一点吗?另外,我想将此视图添加到 UIScrollView,我还要这样做吗? [self addSubview:self.view]; 啊,好吧,我认为问题在于您已将 UIViewController 子类方法复制到 UIView 子类中。 UIView没有- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil的方法@我会编辑我的答案来扩展和回答你的问题 非常感谢,我搞定了。你能解释一下为什么我需要加载笔尖吗?我没有设置它等于任何东西..为什么会有所不同?还有,为什么我们要给 self 添加 self.view?【参考方案2】:

如前所述,initWithNibName 属于控制器。 我想,你走错路了。

你有一个 ViewController 并且想要添加一个 customView,从 URLData 动态加载一个图像。

您有 2 个选项:选项 1: 在 IB 中做所有事情: 在 Interfacebuilder 中编辑/(或添加一个新的)ViewController 的 XIB 文件并添加您喜欢的视图。该文件的所有者是一个 UIViewController 类/子类。像以前一样做所有事情,除了在视图控制器中做。在您的 App 委托 initWithNibName 中,您的 ViewController 就像这样

    self.viewController = [[testViewController alloc] initWithNibName:@"testViewController" bundle:nil];

如果您愿意,可以初始化您自己的视图控制器,您希望将其推送到堆栈中。

您做错了什么:您正在使用 Frame 初始化 customImageView,但笔尖没有自动加载。属性在那里,但笔尖不在。

如果你真的想要一个稳定的东西,请选择选项 2:以编程方式完成所有事情(它更容易、更易理解且轻量级): 从您的实施中,我们执行以下操作:

NewsViewController.m

像以前一样做!!!

CustomImageView.h:

#import <UIKit/UIKit.h>

@interface CustomImageView : UIView

    UIImageView *imageView;
    UILabel *caption;
    UIActivityIndicatorView *activityIndicator;


@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UILabel *caption;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@end

CustomImageView.m:

- (id)initWithFrame:(CGRect)frame

    self = [super initWithFrame:frame];
    if (self) 
        // Initialization code
        caption = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, 320, 50)];
        [caption setBackgroundColor:[UIColor greenColor]];
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
        [imageView setBackgroundColor:[UIColor blueColor]];
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        self.activityIndicator.frame = CGRectMake(320/2-25, 460/2-25, 50, 50);
        [self.activityIndicator startAnimating];


        [self addSubview:self.caption];
        [self addSubview:self.imageView];
        [self addSubview:self.activityIndicator];
    
    return self;

然后做你以前做过的一切。 这样会做得更好

【讨论】:

以上是关于UIView 未显示在 UIScrollView 中的主要内容,如果未能解决你的问题,请参考以下文章

带有子 UIView(保存内容)的 UIScrollView 未检测到点击

UIView 未以编程方式显示在 UIScrollView 中

UIScrollView 不会在 UIView 中显示

UIScrollView 未检测到折叠后的触摸

在 UIScrollView 上显示大 UIView 的最佳方式是啥?

如何知道 UIScrollView 显示了哪些 UIView 索引或标签?