带标签的滚动视图
Scrollview with labels
我有一个带有视图的滚动视图,在该视图上我有一些标签。
现在,当我设置我的约束时,一切正常,除了视图不滚动。
这是我的视图控制器:
#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
有人知道为什么这不滚动吗?
您没有设置 _scrollView.contentSize
。没有 contentSize
滚动视图将不会滚动。它应该大于滚动视图的bounds
。
试试这个
_scrollView.contentSize=CGSizeMake(scrollableWidth,scrollableHeight);
如果您想沿一个方向滚动,请将另一个方向设置为等于其 frame.size.height
或 frame.size.width
的方向。
我有一个带有视图的滚动视图,在该视图上我有一些标签。
现在,当我设置我的约束时,一切正常,除了视图不滚动。
这是我的视图控制器:
#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
有人知道为什么这不滚动吗?
您没有设置 _scrollView.contentSize
。没有 contentSize
滚动视图将不会滚动。它应该大于滚动视图的bounds
。
试试这个
_scrollView.contentSize=CGSizeMake(scrollableWidth,scrollableHeight);
如果您想沿一个方向滚动,请将另一个方向设置为等于其 frame.size.height
或 frame.size.width
的方向。