2013-04-27 110 views
1

我用故事板,我已經創建了一個UIScrollView和它的出口:使用的UIScrollView和Xcode 4.6

.h

@interface ViewController : UIViewController <UIScrollViewDelegate> 
    @property (strong, nonatomic) IBOutlet UIScrollView *scrollView; 
@end 

.m:我合成滾動視圖,並在viewDidLoad中我有

- (void)viewDidLoad { 
[super viewDidLoad]; 
self.scrollView.delegate = self; 

// Init an array with 3 images 
subviewArray = [[NSMutableArray alloc] initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", nil]; 

for (int i = 0; i < [subviewArray count]; i++) { 
    //We'll create a View object in every 'page' of our scrollView. 
    CGRect frame; 
    frame.origin.x = self.scrollView.frame.size.width * i; 
    frame.origin.y = 0; 
    frame.size = self.scrollView.frame.size; 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame]; 
    imageView.image = [UIImage imageNamed:[subviewArray objectAtIndex:i]]; 
    [self.scrollView addSubview:imageView]; 
} 

NSLog(@"%f", self.scrollView.frame.size.width); 

//Set the content size of our scrollview according to the total width of our imageView objects. 
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [subviewArray count], self.scrollView.frame.size.height); 
} 

問題是當我打印「self.scrollView.frame.size.width」的值時,我得到「0」! 任何幫助PLZ!

+0

您是否將滾動視圖連接到插座? self.scrollView是否爲? – Wain 2013-04-27 11:47:20

+0

當然,我做過,當我不使用故事板時,它使用相同的邏輯! – raed 2013-04-27 12:14:32

+0

你確認實例是在運行時設置的嗎? – Wain 2013-04-27 12:41:13

回答

3

修復它,我不得不取消選中文件檢查器中的「使用AutoLayout」。非常感謝Wain!希望這可以幫助其他人。祝你好運 !