-2

I'm having a scrollview with a view and on that view I'm having some labels.

Now when I setup my constraints everything works except the view isn't scrolling.

This is my view controller:

#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

anyone an idea why this isn't scrolling?

user1007522
  • 7,040
  • 14
  • 62
  • 107
  • Where is `_scrollView.contentSize`? Without `contentSize` scroll view wont scroll. – Blind Ninja Sep 08 '15 at 12:50
  • 1
    view will only scroll when its contentSize is great then bounds, so set your contentSize – Adnan Aftab Sep 08 '15 at 12:51
  • 2
    This could not be the best solution but my recommentation about this kinda views are using UITableViewController and creating UITableViewCells for UILabels. Reason why i recommend is you would not be facing with the problem of content size or scrolling again. And using tableView would be more reasonable if you are adding UITextField's to this view cause UITableViewController handles all the keyboardWillHide/Show actions by itself. Food for thought :) – Sabrican Ozan Sep 08 '15 at 12:53
  • Please add your constraints' screenshot. It can be handled just using constraints – Candost Sep 08 '15 at 13:27
  • My constraints are in code. – user1007522 Sep 08 '15 at 14:07

1 Answers1

2

You are not setting _scrollView.contentSize. Without contentSize scroll view wont scroll. It should be larger than bounds of scroll view.

Try this

_scrollView.contentSize=CGSizeMake(scrollableWidth,scrollableHeight);

If you want to scroll in one direction , set another direction to equal to its frame.size.height or frame.size.width whatever it is .

Blind Ninja
  • 1,020
  • 13
  • 27