2015-09-08 5 views
-2

J'ai un scrollview avec une vue et sur cette vue j'ai des étiquettes. Maintenant, lorsque je configure mes contraintes, tout fonctionne, sauf que la vue ne défile pas.Scrollview avec des étiquettes

Ceci est mon contrôleur de vue:

#import <Reliant/NSObject+OCSReliantInjection.h> 
#import "GinDetailViewController.h" 
#import "Gin.h" 
#import "View+MASAdditions.h" 
#import "Country.h" 

@interface GinDetailViewController() 
@property(nonatomic, strong) UILabel *titleContents; 
@property(nonatomic, strong) UILabel *detailContents; 

@property(nonatomic, strong) UILabel *titleAlcohol; 
@property(nonatomic, strong) UILabel *detailAlcohol; 

@property(nonatomic, strong) UILabel *titleOrigin; 
@property(nonatomic, strong) UILabel *detailOrigin; 

@property(nonatomic, strong) UILabel *titleType; 
@property(nonatomic, strong) UILabel *detailType; 

@property(nonatomic, strong) UILabel *titleTaste; 
@property(nonatomic, strong) UILabel *detailTaste; 

@property(nonatomic, strong) UIScrollView *scrollView; 
@property(nonatomic, strong) UIView *container; 
@end 

@implementation GinDetailViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self _inject]; 
    [self _initUI]; 
} 

- (void)_inject { 
    [self ocsInject]; 
} 

- (void)_initUI { 
    self.view.backgroundColor = [UIColor whiteColor]; 
    self.navigationItem.title = @"Details"; 
    [self _setupScrollView]; 
    [self _setupView]; 
    [self _setupContents]; 
    [self _setupAlcohol]; 
    [self _setupOrigin]; 
    [self _setupType]; 
    [self _setupTaste]; 
} 

- (void)_setupScrollView { 
    _scrollView = [[UIScrollView alloc] init]; 
    [self.view addSubview:_scrollView]; 

    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.edges.equalTo(self.view); 
    }]; 
} 

- (void)_setupView { 
    _container = [[UIView alloc] init]; 

    [_scrollView addSubview:_container]; 

    [_container mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.width.equalTo(self.view).offset(-8); 
     make.height.equalTo(self.view).offset(-8); 
     make.top.left.equalTo(self.scrollView).offset(8.0); 
    }]; 
} 

- (void)_setupContents { 
    _titleContents = [[UILabel alloc] init]; 
    _titleContents.text = @"Contents"; 
    [self.container addSubview:_titleContents]; 

    [_titleContents mas_makeConstraints:^(MASConstraintMaker *make) { 
     UIView *topLayoutGuide = (id) self.topLayoutGuide; 
     make.top.equalTo(topLayoutGuide.mas_bottom); 
     make.left.equalTo(self.container).offset(8); 
    }]; 

    _detailContents = [[UILabel alloc] init]; 
    _detailContents.text = _gin.contents; 
    [self.container addSubview:_detailContents]; 

    [_detailContents mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(_titleContents); 
     make.left.equalTo(_titleContents.mas_right).offset(8); 
    }]; 
} 

- (void)_setupAlcohol { 
    _titleAlcohol = [[UILabel alloc] init]; 
    _titleAlcohol.text = @"Alcohol"; 
    [self.container addSubview:_titleAlcohol]; 

    [_titleAlcohol mas_makeConstraints:^(MASConstraintMaker *make) { 
     UIView *topLayoutGuide = (id) self.topLayoutGuide; 
     make.top.equalTo(topLayoutGuide.mas_bottom); 
     make.left.equalTo(self.container.mas_centerX).offset(8); 
    }]; 

    _detailAlcohol = [[UILabel alloc] init]; 
    _detailAlcohol.text = [NSString stringWithFormat:@"%@°", _gin.alcohol]; 
    [self.container addSubview:_detailAlcohol]; 

    [_detailAlcohol mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(_titleAlcohol); 
     make.left.equalTo(_titleAlcohol.mas_right).offset(8); 
    }]; 
} 

- (void)_setupOrigin { 
    _titleOrigin = [[UILabel alloc] init]; 
    _titleOrigin.text = @"Origin"; 
    [self.container addSubview:_titleOrigin]; 

    [_titleOrigin mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(_titleContents.mas_bottom).offset(8); 
     make.left.equalTo(_titleContents); 
    }]; 

    _detailOrigin = [[UILabel alloc] init]; 
    NSArray *countries = [_gin.origin allObjects]; 
    if (countries.count > 0) { 
     Country *country = countries[0]; 
     _detailOrigin.text = country.name; 
    } 
    [self.container addSubview:_detailOrigin]; 

    [_detailOrigin mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(_titleOrigin); 
     make.left.equalTo(_titleOrigin.mas_right).offset(8); 
    }]; 
} 

- (void)_setupType { 
    _titleType = [[UILabel alloc] init]; 
    _titleType.text = @"Type"; 
    [self.container addSubview:_titleType]; 

    [_titleType mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(_titleAlcohol.mas_bottom).offset(8); 
     make.left.equalTo(self.container.mas_centerX).offset(8); 
    }]; 

    _detailType = [[UILabel alloc] init]; 
    _detailType.text = _gin.type; 
    [self.container addSubview:_detailType]; 

    [_detailType mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(_titleType); 
     make.left.equalTo(_titleType.mas_right).offset(8); 
    }]; 
} 

- (void)_setupTaste { 
    _titleTaste = [[UILabel alloc] init]; 
    _titleTaste.text = @"Taste"; 
    [self.container addSubview:_titleTaste]; 

    [_titleTaste mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(_titleOrigin.mas_bottom).offset(16); 
     make.left.equalTo(self.container).offset(8); 
    }]; 

    _detailTaste = [[UILabel alloc] init]; 
    _detailTaste.lineBreakMode = NSLineBreakByWordWrapping; 
    //_detailTaste.text = _gin.taste; 
    _detailTaste.text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dictum porta mattis. Cras efficitur efficitur orci at ultrices. Integer faucibus fermentum arcu vitae dapibus. Maecenas mollis augue placerat tellus ultrices, auctor congue dolor efficitur. Proin semper velit venenatis justo volutpat, vel fermentum ipsum pellentesque. Integer tempus at ex ut ultrices. Etiam varius tempor eros hendrerit gravida. Nunc placerat felis sit amet magna faucibus molestie. Curabitur iaculis euismod gravida. Maecenas vehicula dolor orci, ac interdum turpis gravida a. Suspendisse eleifend ante vel odio faucibus, non ultrices odio ornare. Etiam interdum justo convallis sem fermentum posuere. Pellentesque ut orci tincidunt, feugiat lectus a, volutpat arcu.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dictum porta mattis. Cras efficitur efficitur orci at ultrices. Integer faucibus fermentum arcu vitae dapibus. Maecenas mollis augue placerat tellus ultrices, auctor congue dolor efficitur. Proin semper velit venenatis justo volutpat, vel fermentum ipsum pellentesque. Integer tempus at ex ut ultrices. Etiam varius tempor eros hendrerit gravida. Nunc placerat felis sit amet magna faucibus molestie. Curabitur iaculis euismod gravida. Maecenas vehicula dolor orci, ac interdum turpis gravida a. Suspendisse eleifend ante vel odio faucibus, non ultrices odio ornare. Etiam interdum justo convallis sem fermentum posuere. Pellentesque ut orci tincidunt, feugiat lectus a, volutpat arcu.\n" 
      ""; 
    _detailTaste.numberOfLines = 0; 
    [self.container addSubview:_detailTaste]; 

    [_detailTaste mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(_titleTaste.mas_bottom).offset(8); 
     make.left.equalTo(self.container).offset(8); 
     make.right.equalTo(self.container).offset(-8); 
    }]; 
} 


@end 

quelqu'un une idée pourquoi cela ne défile pas?

+0

Où est '_scrollView.contentSize'? Sans l'affichage défilement 'contentSize' ne défilera pas. La vue –

+1

ne défilera que si contentSize est content, alors définissez votre contentSize –

+2

Cela ne peut pas être la meilleure solution, mais ma recom- mandation à propos de ce type de vue utilise UITableViewController et crée UITableViewCells pour UILabels. Raison pour laquelle je recommande est que vous ne seriez pas confronté au problème de la taille du contenu ou de défilement à nouveau. Et l'utilisation de tableView serait plus raisonnable si vous ajoutez UITextField à cette vue car UITableViewController gère toutes les actions keyboardWillHide/Show par lui-même. Matière à réflexion :) –

Répondre

2

Vous ne définissez pas _scrollView.contentSize. Sans contentSize défilement ne défile pas. Il devrait être plus grand que bounds de la vue déroulante.

Essayez cette

_scrollView.contentSize=CGSizeMake(scrollableWidth,scrollableHeight); 

Si vous voulez faire défiler dans un sens, définir une autre direction égale à sa frame.size.height ou frame.size.width quoi que ce soit.

+0

la largeur ou la hauteur dois-je remplir une valeur élevée aléatoire ou puis-je calculer cela? – user1007522

+0

Offcours vous devez le calculer. –