2012-09-14 28 views
1

我有一個啓用了分頁的UIScrollView。我想充滿圖像,並且我有一個UIImageView,需要在不使用.frame的情況下進行安裝。這是因爲它不適用於啓用Autolayout。不使用Autolayout不是一種選擇。UIImageView無框架定義大小

下面的代碼,因爲它主張:

//Prepare and load the view 
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), (kScrollObjHeight))]; 
scrollView1.pagingEnabled = YES; 
[scrollView1 setShowsHorizontalScrollIndicator:NO]; 

CGRect Rect = scrollView1.bounds; 
UIImageView *beadContainer; 

for (int i = 0; i < imageViews.count; i++) 
{ 
    beadContainer = [imageViews objectAtIndex:i]; 
    beadContainer.frame = Rect; 
    [scrollView1 addSubview:beadContainer]; 
    Rect.origin.x += Rect.size.width; 
} 

圖像中沒有出現,因爲它代表,雖然滾動型擁有所有正確的尺寸和滾動的預期。如果我註釋掉beadContainer.frame = Rect;,那麼數組imageViews中的所有圖像出現在0, 0處。它們都顯示在另一個之上。當然,我需要他們來填充ScrollView。

回答

2

爲什麼不使用自動佈局佈置圖像?

//Prepare and load the view 
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), (kScrollObjHeight))]; 
scrollView1.pagingEnabled = YES; 
[scrollView1 setShowsHorizontalScrollIndicator:NO]; 



UIImageView *firstImage = imageViews[0]; 
UIImageView *lastImageView = [imageViews lastObject]; 
UIImageView *previousImageView = nil; 
NSMutableArray *constraints = [NSMutableArray new]; 
for (UIImageView *imageView in imageViews){ 
    [scrollView1 addSubview:imageView]; 
    //set the size of the images 
    [constraints addObject:[NSLayoutConstraint constraintWithItem:imageView 
                 attribute:NSLayoutAttributeWidth 
                 relatedBy:NSLayoutRelationEqual 
                  toItem:nil 
                 attribute:NSLayoutAttributeNotAnAttribute 
                 multiplier:1.0f 
                 constant:kScrollObjWidth]]; 
    [constraints addObject:[NSLayoutConstraint constraintWithItem:imageView 
                 attribute:NSLayoutAttributeHeight 
                 relatedBy:NSLayoutRelationEqual 
                  toItem:nil 
                 attribute:NSLayoutAttributeNotAnAttribute 
                 multiplier:1.0f 
                 constant:kScrollObjHeight]]; 
    //remove autoresizing masks 
    [imageView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    //pin the top of the imageview to the top of the superview 
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintWithVisualFormat:@"V:|[imageView]" 
                      options:0 
                      metrics:nil 
                       views:NSDictionaryOfVariableBindings(imageView)]]; 

    if ([firstImage isEqual:imageView]){ //pin the first image view to the left of the scrollview 
     [constraints addObjectsFromArray:[NSLayoutConstraint constraintWithVisualFormat:@"|[firstImage]" 
                       options:0 
                       metrics:nil 
                        views:NSDictionaryOfVariableBindings(firstImage)]]; 
    } 

    if (previousImageView){ //pin any imageViews to the previous imageViews 
     [constraints addObjectFromArray:[NSLayoutConstraint constraintWithVisualFormat:@"[previousImageView][imageView]" 
                       options:0 
                       metrics:nil 
                       views:NSDictionaryOfVariableBindings(imageView, previousImageView)]]; 
    } 
    previousImageView = imageView; 
} 

[scrollView1 addConstraints:constraints]; 

請注意,我沒有試過這個,它可能是垃圾代碼,我在文本編輯器中編寫它。

+0

哇。謝謝你寫這篇文章。我會修補它,看看它是否完成了工作。思想限制和自動佈局應該讓我的生活變得更輕鬆,但這一直沒有蛋糕走路。再次感謝,我會報告回來! – Sneagan

+0

代碼可能很長,但與通過數學設置框架相比,它們實際上更易於閱讀和理解。尤其是,但在你的情況下似乎並不是這樣,你在每個視圖之間設置了一些填充,所有的小事情加起來就是一個混亂的集團。添加空格的代碼與上面的代碼沒有多大區別,只需將[previousImageView] [imageView]更改爲[previousImageView] - [imageView] –

+0

經過這些後,它使得LOT更有意義。你是對的。更具可讀性。但有一個問題。在熨平了一些扭曲之後,我再也不能與圖像互動了。你有什麼想法可能是爲什麼?我會繼續研究它,並會在我修復它時報告。 – Sneagan

1

如果您要完全使用自動佈局,請不要設置滾動視圖的contentSize。只需充分利用約束來佈置內容,確保描述每個內容子視圖的大小,以及內容子視圖與圍繞它們的虛構矩形邊界的距離。運行時將自動使用該虛構的矩形創建內容大小。