2014-09-12 17 views
47

पर काम नहीं कर रहा है मेरे पास एक सरल UICollectionView है जिसमें एक एकल UITextView है। UITextView सेल के किनारों पर बाध्य है, इसलिए उन्हें सेल आकार के समान आकार में रहना चाहिए।UICollectionViewCell में ऑटो लेआउट

मेरी समस्या यह है कि किसी कारण से ये बाधाएं काम नहीं कर रही हैं जब मैं संग्रह के माध्यम से सेल आकार निर्दिष्ट करता हूं: लेआउट: sizeForItemAtIndexPath:।

मेरे पास स्टोरीबोर्ड में सेल आकार 320x50 पर सेट है। यदि मैं आकार आकार के साथ सेल आकार की 2 गुना ऊंचाई के साथ एक आकार वापस करता हूं, तो IITextView सेट की गई बाधाओं के बावजूद एक ही ऊंचाई पर रहता है। मैं एक्सकोड 6 जीएम का उपयोग कर रहा हूँ।

मेरा विचार नियंत्रक कोड है:

@implementation TestViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.collectionView.delegate = self; 
    self.collectionView.dataSource = self; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    UICollectionViewCell *c = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; 

    NSLog(@"%f", c.frame.size.height); 

    UITextView *tv = (UITextView *)[c viewWithTag:9]; 
    NSLog(@"%f", tv.frame.size.height); 

} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 1; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 

    return cell; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout; 

    CGSize size = flowLayout.itemSize; 

    size.height = size.height * 2; 

    return size; 
} 

@end 

viewDidAppear उन लॉग: मुझे का उत्पादन दे:
100,00000
50,00000
आप देख सकते हैं, UITextView ऊंचाई नहीं है सेल ऊंचाई के साथ बदलें।


यहाँ स्टोरीबोर्ड मैं एक UITextView UICollectionViewCell में विवश के साथ स्थापित की एक स्क्रीनशॉट:

enter image description here

मैं ऑटो लेआउट की कमी का उपयोग कर पता UITableViewCells और गतिशील आकार बदलने के साथ ठीक काम करता है। मुझे नहीं पता कि यह इस मामले में क्यों काम नहीं कर रहा है। क्या किसी के पास कोई विचार है?

उत्तर

109

ठीक है, मैंने अभी आईओएस डेवलपर मंचों को देखा है। जाहिर है यह आईओएस 8 एसडीके आईओएस 7 डिवाइस पर चल रहा एक बग है। वैकल्पिक हल UICollectionViewCell के अपने उपवर्ग के लिए निम्नलिखित जोड़ने के लिए है:

- (void)setBounds:(CGRect)bounds { 
    [super setBounds:bounds]; 
    self.contentView.frame = bounds; 
} 
+1

बढ़िया! तुम मेरा दिन बचाओ! आईओएस 8 एक्सकोड 6 आदि स्थापित करने के बाद यह समाधान मेरे लिए काम कर रहा है। मेरे सभी टेबलव्यू ने यूकोलेक्लेव्यूव्यू सेल आकार को अनुकूलित नहीं किया है। धन्यवाद :) – Fjohn

+1

यदि यह आपके लिए काम नहीं करता है तो कोशिश करें: awakerFromNib() {contentView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight} –

+0

आईओएस 8 के साथ आईपैड पर मेरा एक ही मुद्दा था, और इस समाधान ने समस्या को भी ठीक किया। धन्यवाद! – almas

23

यहाँ, समाधान है अगर आप UICollectionViewCell उपवर्गीकरण नहीं कर रहे हैं। बस dequeueReusableCellWithReuseIdentifier:

Obj सी

[[cell contentView] setFrame:[cell bounds]]; 
[[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; 

स्विफ्ट के बाद अपने cellForItemAtIndexPath: के तहत इस दो लाइनों को जोड़ने - 2,0

cell.contentView.frame = cell.bounds 
cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 
+1

मेरे लिए यह कोड –

+0

से दूसरी पंक्ति जोड़ने के बिना काम करता है यदि आपका ऐप एकाधिक अभिविन्यास को श्रेष्ठ करता है, तो आपको दूसरी पंक्ति भी चाहिए। :-) @ टीना नाथ पॉल – itsji10dra

40

समतुल्य स्विफ़्ट कोड:

override var bounds: CGRect { 
    didSet { 
     contentView.frame = bounds 
    } 
} 
+2

क्या आप कृपया मुझे बता सकते हैं कि नीचे वोट क्यों, धन्यवाद :) क्योंकि मुझे लगता है कि यह उत्तर नए त्वरित डेवलपर्स के लिए उपयोगी हो सकता है, खासकर अगर वे उद्देश्य-सी –

+0

से अपरिचित हैं, तो यह उत्तर है सहायक, लेकिन स्वीकार्य उत्तर का अनुवाद है। शायद यही वह व्यक्ति है जिसने इस जवाब को वोट दिया था। – cbh2000

+0

यह वास्तव में मेरी मदद की ... धन्यवाद –

4

स्विफ्ट 2.0:

cell.contentView.frame = cell.bounds 
cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 
+1

इसे 'cellForItemAt' में जोड़ा गया और कुछ भी नहीं बदला :( – ixany

संबंधित मुद्दे