Ios6 将子视图添加到自定义视图时出现问题

Ios6 将子视图添加到自定义视图时出现问题,ios6,Ios6,我试图创建一个页面滚动视图,显示下一个和上一个图像 我创建了一个自定义UIView类,并将UIScrollView添加为子视图。我有两个问题 我希望视图与IB一起工作,因此我实现了initWithCoder,并在调用超类initWithCoder之后尝试将帧传递给我的init。但帧的宽度和高度始终为0 为了解决这个问题,我在initView方法中使用了屏幕宽度。但是我的scrollview(或者更确切地说是其中的图像)没有显示。我改变了自定义视图(红色)和滚动视图(蓝色)的背景色,我看到的只是红

我试图创建一个页面滚动视图,显示下一个和上一个图像

我创建了一个自定义UIView类,并将UIScrollView添加为子视图。我有两个问题

  • 我希望视图与IB一起工作,因此我实现了initWithCoder,并在调用超类initWithCoder之后尝试将帧传递给我的init。但帧的宽度和高度始终为0
  • 为了解决这个问题,我在initView方法中使用了屏幕宽度。但是我的scrollview(或者更确切地说是其中的图像)没有显示。我改变了自定义视图(红色)和滚动视图(蓝色)的背景色,我看到的只是红色背景。我已经检查了scrollview的frame rect,它看起来很合理(59,0,201,135),所以我不确定为什么不能显示scrollview 如有任何想法,将不胜感激

    模式选择View.m

    #import "ModeSelectView.h"
    
    @implementation ModeSelectView
    
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self initView:frame];
        }
        return self;
    }
    
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:(aDecoder)];
        if (self) {
            CGRect frame = self.frame;
            [self initView:self.frame];
        }
        return self;
    }
    
    - (void)initView:(CGRect)frame
    {
        // Initialization code
        // Create an array of images for the different modes
        UIImage *imgTowerOnlyMode = [UIImage imageNamed:@"tower_only_mode_icon.png"];
        UIImage *imgLocalMode = [UIImage imageNamed:@"local_mode_icon.png"];
        UIImage *imgHowToPlay = [UIImage imageNamed:@"how_to_play_icon.png"];
        NSArray *modeSelectIcons = @[imgTowerOnlyMode, imgLocalMode, imgHowToPlay];
    
        int iconSize = 115;
        // Center to center icon spacing
        int viewSpacing = 115 * 1.75;
        int frameWidth = 2*viewSpacing;
        int contentWidth = 4*frameWidth;
        int pageWidth = viewSpacing;
        int verticalPadding = 10;
        int pageHeight = iconSize + 2*verticalPadding;
            //int viewWidth = frame.size.width;
        int viewWidth = [[UIScreen mainScreen] applicationFrame].size.width;
        int scrollViewX = (viewWidth - pageWidth)/2;
    
        // Create the scrollview, setting it to be page size in width the icon height plus padding
        CGRect scrollViewFrame = CGRectMake(scrollViewX, 0, pageWidth, pageHeight);
        self.scrollModeSelect = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
    
        // Now iterate over the array creating a view for each
        // The first view will be offset in X to allow it to be
        // centered in the page
        int imageOffset = (frameWidth-iconSize)/2;
        for (int i = 0; i < [modeSelectIcons count]; ++i) {
            // Get the origin x value for the image view within the scroll view
            int viewOriginX = i*frameWidth + imageOffset;
    
            // Initialize the image view
            UIImageView *ivModeSelect = [[UIImageView alloc]
            initWithFrame:CGRectMake(viewOriginX , verticalPadding/2,
                                     iconSize, iconSize)];
    
            // Set the image
            ivModeSelect.image = (UIImage *)modeSelectIcons[i];
    
            // Tell the parent view to scale the iamge, preserving aspect ratio, to
            // fit the parent view.
            ivModeSelect.contentMode = UIViewContentModeScaleAspectFit;
    
            // Add the image view to the scroll view
            [self.scrollModeSelect addSubview:ivModeSelect];
        }
    
        [self.scrollModeSelect setContentSize:CGSizeMake(contentWidth, pageHeight)];
        // Turn off clipping so we can see the adjacent icons
        [self.scrollModeSelect setClipsToBounds:FALSE];
    
        // Add the scrollview as a subview
        [self addSubview:self.scrollModeSelect];
    
        [self.scrollModeSelect setBackgroundColor:[UIColor blueColor]];
        [self setBackgroundColor:[UIColor redColor]];
    }
    
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */
    
    @end
    
    #导入“ModeSelectView.h”
    @实现模式选择视图
    -(id)initWithFrame:(CGRect)帧
    {
    self=[super initWithFrame:frame];
    如果(自我){
    [自初始化视图:帧];
    }
    回归自我;
    }
    -(id)initWithCoder:(NSCoder*)aDecoder
    {
    self=[super initWithCoder:(aDecoder)];
    如果(自我){
    CGRect frame=self.frame;
    [self initView:self.frame];
    }
    回归自我;
    }
    -(void)initView:(CGRect)帧
    {
    //初始化代码
    //为不同模式创建图像数组
    UIImage*imgTowerOnlyMode=[UIImage ImageName:@“仅塔模式图标.png”];
    UIImage*imgLocalMode=[UIImage ImageName:@“local_mode_icon.png”];
    UIImage*imgHowToPlay=[UIImage ImageName:@“如何播放图标.png”];
    NSArray*modeSelectIcons=@[imgTowerOnlyMode、imgLocalMode、imgHowToPlay];
    int iconSize=115;
    //中心到中心图标间距
    内部视图间距=115*1.75;
    int frameWidth=2*视图间距;
    int contentWidth=4*frameWidth;
    int pageWidth=视图间距;
    int垂直填充=10;
    int pageHeight=iconSize+2*垂直填充;
    //int viewWidth=frame.size.width;
    int viewWidth=[[UIScreen mainScreen]applicationFrame].size.width;
    int scrollViewX=(viewWidth-pageWidth)/2;
    //创建滚动视图,将其设置为页面宽度大小图标高度加上填充
    CGRect scrollViewFrame=CGRectMake(scrollViewX,0,pageWidth,pageHeight);
    self.scrollModeSelect=[[UIScrollView alloc]initWithFrame:scrollViewFrame];
    //现在迭代数组,为每个数组创建一个视图
    //第一个视图将在X轴上偏移,以允许
    //居中
    int imageOffset=(帧宽度图标大小)/2;
    对于(int i=0;i<[modeSelectIcons count];++i){
    //获取滚动视图中图像视图的原点x值
    int viewOriginX=i*帧宽+图像偏移;
    //初始化图像视图
    UIImageView*ivModeSelect=[[UIImageView alloc]
    initWithFrame:CGRectMake(viewOriginX,垂直填充/2,
    i consize,i consize)];
    //设置图像
    ivModeSelect.image=(UIImage*)modeSelectIcons[i];
    //告诉父视图将iamge缩放,保持纵横比为
    //适合父视图。
    ivModeSelect.contentMode=UIViewContentModeScaleAspectFit;
    //将图像视图添加到滚动视图中
    [self.scrollModeSelect addSubview:ivModeSelect];
    }
    [self.scrollModeSelect setContentSize:CGSizeMake(contentWidth,pageHeight)];
    //关闭剪辑,以便我们可以看到相邻的图标
    [self.scrollModeSelect setclipstobunds:FALSE];
    //将滚动视图添加为子视图
    [self addSubview:self.scrollModeSelect];
    [self.scrollModeSelect setBackgroundColor:[UIColor blueColor]];
    [self-setBackgroundColor:[UIColor redColor];
    }
    /*
    //仅覆盖drawRect:如果执行自定义绘图。
    //空实现会对动画期间的性能产生不利影响。
    -(void)drawRect:(CGRect)rect
    {
    //绘图代码
    }
    */
    @结束
    
    事实证明,由于应用程序框架尚未确定,因此无法在initWith方法中进行任何视图大小调整。因此,要做到这一点,您需要在视图层次结构中使用默认大小设置视图。然后,您需要覆盖LayoutSubView来进行大小调整。在布局子视图中,可以使用self.frame访问图幅。下面是与上面相同的代码,但是使用了LayoutSubview

    - (void)initView:(CGRect)frame
    {
        // Initialization code
        self.ivModeSelectArray = [[NSMutableArray alloc] init];
    
        // Create an array of images for the different modes
        UIImage *imgTowerOnlyMode = [UIImage imageNamed:@"tower_only_mode_icon.png"];
        UIImage *imgLocalMode = [UIImage imageNamed:@"local_mode_icon.png"];
        UIImage *imgHowToPlay = [UIImage imageNamed:@"how_to_play_icon.png"];
        NSArray *modeSelectIcons = @[imgTowerOnlyMode, imgLocalMode, imgHowToPlay];
    
        // Create the scrollview, initially setting it to 0 size.  We'll resize it
        // in layoutSubviews
        CGRect scrollViewFrame = CGRectMake(0,0,200,110);
        self.scrollModeSelect = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
    
        // Now iterate over the array creating a view for each, intially set to 0
        // size.  We'll resize them and reposition them in layoutSubviews
        for (int i = 0; i < [modeSelectIcons count]; ++i) {
            UIImageView *ivModeSelect =
                     [self addModeIcon:[modeSelectIcons objectAtIndex:i]];
    
            // Add the image view to the scroll view
            [self.scrollModeSelect addSubview:ivModeSelect];
        }
    
        // Turn off clipping so we can see the adjacent icons
        [self.scrollModeSelect setClipsToBounds:FALSE];
    
        // Turn on paging
        [self.scrollModeSelect setPagingEnabled:TRUE];
    
        // Add the scrollview as a subview
        [self addSubview:self.scrollModeSelect];
    
    //    self.scrollModeSelect.backgroundColor = [UIColor blueColor];
    //    self.backgroundColor = [UIColor redColor];
    }
    
    - (void)layoutSubviews
    {
        int iconSize = 115;
        // Center to center icon spacing
        CGFloat viewSpacing = 115 * 1.4;
        int frameWidth = viewSpacing;
        int contentWidth = 4*frameWidth;
        int pageWidth = viewSpacing;
        int verticalPadding = 10;
        int pageHeight = iconSize + 2*verticalPadding;
        int viewWidth = self.frame.size.width;
        int scrollViewX = (viewWidth - frameWidth)/2;
    
        // Now iterate over the array configuring the size and offset for each view
        // The first view will be offset in X to allow it to be centered in the page
        int imageOffset = (frameWidth-iconSize)/2;
        for (int i = 0; i < [self.ivModeSelectArray count]; ++i) {
            // Get the origin x value for the image view within the scroll view
            int viewOriginX = i*viewSpacing + imageOffset;
    
            // Size the image view
            UIImageView *ivModeSelect = [self.ivModeSelectArray objectAtIndex:i];
            [ivModeSelect setFrame:CGRectMake(viewOriginX , verticalPadding/2,
                                              iconSize, iconSize)];
    
        }
    
        [self.scrollModeSelect setFrame:CGRectMake(scrollViewX, 0,
                                                   pageWidth,pageHeight)];
        [self.scrollModeSelect setContentSize:CGSizeMake(contentWidth, pageHeight)];
    }
    
    -(void)initView:(CGRect)帧
    {
    //初始化代码
    self.ivModeSelectArray=[[NSMutableArray alloc]init];
    //为不同模式创建图像数组
    UIImage*imgTowerOnlyMode=[UIImage ImageName:@“仅塔模式图标.png”];
    UIImage*imgLocalMode=[UIImage ImageName:@“local_mode_icon.png”];
    UIImage*imgHowToPlay=[UIImage ImageName:@“如何播放图标.png”];
    NSArray*modeSelectIcons=@[imgTowerOnlyMode、imgLocalMode、imgHowToPlay];
    //创建滚动视图,最初将其设置为0大小。我们将调整其大小
    //在布局子视图中
    CGRect scrollViewFrame=CGRectMake(0,0200110);
    self.scrollModeSelect=[[UIScrollView alloc]initWithFrame:scrollViewFrame];
    //现在迭代数组,为每个数组创建一个视图,初始设置为0
    //大小。我们将调整它们的大小并在布局子视图中重新定位它们
    对于(int i=0;i<[modeSelectIcons count];++i){
    UIImageView*ivModeSelect=
    [自添加模式图标:[模式选择图标对象索引:i]];
    //将图像视图添加到滚动视图中
    [self.scrollModeSelect addSubview:ivModeSelect];
    }
    //关闭剪辑,以便我们可以看到相邻的图标
    [self.scrollModeSelect setclipstobunds:FALSE];
    //打开分页
    [self.scrollModeSelect setPaginEnabled:TRUE];
    //将滚动视图添加为子视图
    [self addSubview:self.scrollModeSelect];
    //self.scrollModeSelect.backgroundColor=[UIColor blueColor];
    //自我背景