【发布时间】:2015-09-08 12:46:51
【问题描述】:
我有一个带有视图的滚动视图,并且在该视图上我有一些标签。
现在,当我设置约束时,一切正常,除了视图不滚动。
这是我的视图控制器:
#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滚动视图不会滚动。 -
视图只会在其 contentSize 大到边界时滚动,所以设置你的 contentSize
-
这可能不是最好的解决方案,但我对这种视图的建议是使用 UITableViewController 并为 UILabel 创建 UITableViewCells。我推荐的原因是您不会再面临内容大小或滚动的问题。如果您将 UITextField 添加到此视图中,使用 tableView 会更合理,因为 UITableViewController 会自行处理所有的keyboardWillHide/Show 操作。值得深思:)
-
请添加您的约束截图。它可以只使用约束来处理
-
我的约束在代码中。
标签: ios objective-c masonry