2013-01-12 56 views
0

我試圖按照this answer創建一個分頁滾動視圖來顯示下一個和上一個圖像。將子視圖添加到自定義視圖時出現的問題

我創建了一個自定義UIView類,並添加了UIScrollView作爲子視圖。我有兩個問題。

  1. 我希望視圖能夠與IB一起工作,所以我實現了initWithCoder,並在調用超類initWithCoder之後嘗試將框架傳遞給我的init。但框架始終爲0寬度和高度。
  2. 爲了解決這個問題,我只在我的initView方法中使用了屏幕寬度。但我的滾動視圖(或內部的圖像)不顯示。我改變了自定義視圖(紅色)和滾動視圖(藍色)的背景顏色,我看到的只是紅色背景。我檢查了scrollview的frame rect,它看起來很合理(59,0,201,135),所以我不確定爲什麼我不能顯示scrollview。

任何想法將不勝感激。

ModeSelectView.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 

回答

0

事實證明,你真的不能做任何視圖大小在initWith方法,因爲應用程序框架尚未確定。所以要做到這一點,你需要在默認大小的視圖層次結構中設置你的視圖。那麼你需要重寫layoutSubviews來做你的尺寸。在layoutSubviews中,您可以使用self.frame訪問您的視圖框架。所以這裏是與上面相同的代碼,但使用layoutSubviews。

- (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)]; 
} 
相關問題