gpt4 book ai didi

ios6 - 我的 UIScrollView 不适用于 ios6 中的自动布局

转载 作者:行者123 更新时间:2023-12-02 08:42:44 25 4
gpt4 key购买 nike

我已将 UIViewController 中的 UIScrollView 放入 Storyboard中。当我使用这段代码时:

- (void)viewDidLoad
{
[super viewDidLoad];

[_scrollview setContentSize:CGSizeMake(_scrollview.bounds.size.width*2, _scrollview.bounds.size.height)];
[_scrollview setPagingEnabled:YES];

CGRect rect = _scrollview.bounds;

UIView* view = [[UIView alloc]initWithFrame:rect];
[view setBackgroundColor:[UIColor redColor]];
[_scrollview addSubview:view];

rect = CGRectOffset(rect, _scrollview.bounds.size.width, 0);
view = [[UIView alloc]initWithFrame:rect];
view.backgroundColor = [UIColor greenColor];
[_scrollview addSubview:view];

}

它在没有自动布局的情况下工作正常,但是当我启用时,“rect”值等于 0。自动布局的等效代码是什么?

最佳答案

似乎您在自动布局环境中缺少有关 UIScrollView 的一些基本内容。仔细阅读ios 6.0 release notes

您的代码应如下所示:

- (void)viewDidLoad
{
[super viewDidLoad];

CGRect selfBounds = self.view.bounds;
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
[_scrollview setPagingEnabled:YES];

UIView* view1 = [[UIView alloc] initWithFrame:selfBounds];
[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[view1 setBackgroundColor:[UIColor redColor]];
[_scrollview addSubview:view1];

UIView* view2 = [[UIView alloc]initWithFrame:CGRectOffset(selfBounds, width, 0)];
[view2 setTranslatesAutoresizingMaskIntoConstraints:NO];
view2.backgroundColor = [UIColor greenColor];
[_scrollview addSubview:view2];

[_scrollview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[view1(width)][view2(width)]|" options:0 metrics:@{@"width":@(width)} views:NSDictionaryOfVariableBindings(view1,view2)]];
[_scrollview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1(height)]|" options:0 metrics:@{@"height":@(height)} views:NSDictionaryOfVariableBindings(view1)]];
[_scrollview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view2(height)]|" options:0 metrics:@{@"height":@(height)} views:NSDictionaryOfVariableBindings(view2)]];
}

关于ios6 - 我的 UIScrollView 不适用于 ios6 中的自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15153407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com