ios - 使用纯自动布局的 UIScrollView 不向右扩展

标签 ios objective-c ios6 uiscrollview nslayoutconstraint

我已经阅读了 Apple 关于自动布局和 UIScrollView 的官方文档:

RN-iOSSDK-6_0

我正在尝试“纯自动布局方法”

我正在添加一个 UILabel 和一个 UITextField。我希望两者都扩展到界面的宽度:

RootViewController.h

@interface rootViewController : UIViewController

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *bigLetters;
@property (nonatomic, strong) UITextField *aTextField;


@end

viewDidLoad

- (void)viewDidLoad {
[super viewDidLoad];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
self.view.backgroundColor = [UIColor lightGrayColor];
_scrollView = [[UIScrollView alloc] init];
[_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
_contentView = [[UIView alloc] init];
[_contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
_bigLetters = [[UILabel alloc] init];
[_bigLetters setTranslatesAutoresizingMaskIntoConstraints:NO];
_bigLetters.font = [UIFont systemFontOfSize:30];
_bigLetters.textColor = [UIColor blackColor];
_bigLetters.backgroundColor = [UIColor clearColor];
_bigLetters.text = @"Why so small?";
_bigLetters.textAlignment = NSTextAlignmentCenter;
_aTextField = [[UITextField alloc] init];
_aTextField.backgroundColor = [UIColor whiteColor];
_aTextField.placeholder = @"A Text Box";
[_aTextField setTranslatesAutoresizingMaskIntoConstraints:NO];


[self.view addSubview:_scrollView];
[self.scrollView addSubview:_contentView];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_contentView)]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_contentView)]];

[self.contentView addSubview:_bigLetters];
[self.contentView addSubview:_aTextField];

[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_bigLetters]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_bigLetters)]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_aTextField]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_aTextField)]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_bigLetters]-[_aTextField]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_bigLetters, _aTextField)]];
// Do any additional setup after loading the view.
}

我将两个元素都压在左上角,如下所示:

enter image description here

如果我将 _scrollView 切换为 UIView,一切看起来都很好。我做错了什么?

谢谢!

最佳答案

UIScrollView 有点复杂。 首先:

[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];

你不应该那样做。您的 Root View 不需要使用自动布局。您的 subview 需要它。

此外, ScrollView 的 contentSize 由您放入其中的内容定义。如果你想要一个 scrollView 屏幕大小的修复它的 subview 之一到屏幕宽度(或八)。 你在那里看到的是你的 ScrollView 包裹它的内容,宽度可能是标签固有内容的大小。

这就是它与 View 一起工作的原因。 希望对您有所帮助!!

关于ios - 使用纯自动布局的 UIScrollView 不向右扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16867353/

相关文章:

ios - 为 Swift 和 Objective-C 类定义扩展

ios - 如何使图像仅在 iPhone 6/6s 上可见

iphone - 如何动态调整 scrollViewDidZoom 中的 contentInset?我最初在 UIScrollview 中设置了 UIImageView 的 contentInset

iphone - 我如何计算从我现在所在的位置到特定点的距离

iphone - 在 iPhone 中安装 ipa 文件,而不将其设备 ID 添加到配置文件中

ios - 在 Xcode 中强制 3.5"屏幕模式

ios - 检查 TextField 不为空后如何启用 UIBarItem

iphone - 如何在AVPlayer播放时开始播放下一首歌曲或识别歌曲已结束?

ios - 在 iOS 6 中使用 Google Direction api

iphone - 使用 SDK 登录 facebook