Ios 基于在具有自动布局的故事板中创建的另一个视图,使用框架以编程方式创建视图
我在情节提要中添加了一个滚动视图,并在情节提要中设置了它的自动布局属性。很明显,不同的iPhone需要不同的尺寸,这正是我想要的。现在我想在Scroll视图中添加两个子视图,每个子视图占据Scroll视图的整个可见区域,并且启用了分页。因此,在任何时间点,其中一个子视图都是可见的,您可以向左或向右滑动以查看另一个子视图 我的问题是,因为我在程序中创建这两个视图,所以我不确定应该在哪里设置子视图框架。这就是我现在在viewDidLayoutSubviews中所做的,但理想情况下,我希望在viewDidload中执行此操作,但在viewDidload中,帧尚未设置:Ios 基于在具有自动布局的故事板中创建的另一个视图,使用框架以编程方式创建视图,ios,objective-c,iphone,uiscrollview,ios-autolayout,Ios,Objective C,Iphone,Uiscrollview,Ios Autolayout,我在情节提要中添加了一个滚动视图,并在情节提要中设置了它的自动布局属性。很明显,不同的iPhone需要不同的尺寸,这正是我想要的。现在我想在Scroll视图中添加两个子视图,每个子视图占据Scroll视图的整个可见区域,并且启用了分页。因此,在任何时间点,其中一个子视图都是可见的,您可以向左或向右滑动以查看另一个子视图 我的问题是,因为我在程序中创建这两个视图,所以我不确定应该在哪里设置子视图框架。这就是我现在在viewDidLayoutSubviews中所做的,但理想情况下,我希望在viewD
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) UIView *pageOne;
@property (nonatomic, strong) UIView *pageTwo;
@end
- (void) viewDidLayoutSubviews
{
self.scrollView.pagingEnabled = YES;
self.pageOne = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height)];
self.pageOne.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.pageOne];
self.pageTwo = [[UIView alloc] initWithFrame: CGRectMake(self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height)];
self.pageTwo.backgroundColor = [UIColor blueColor];
[self.scrollView addSubview:self.pageTwo];
self.statsScrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width * 2, self.scrollView.bounds.size.height);
}
所以,我的问题是,如果我在程序中创建的视图(上面的第1页和第2页)是指序列图像板中视图集的框架,并且它们的大小具有自动布局,那么我应该在哪里设置代码。我知道我的代码有执行多次的问题,这不是我想要的 您必须等到实际布局完成后才能抓取帧,因此
viewdilayoutsubviews
是一个不错的选择。如果要确保只添加一次页面,只需检查页面是否已经初始化,如下所示:
if (!self.pageOne) {
self.pageOne = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height)];
self.pageOne.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.pageOne];
}
当然,您也可以在viewDidLoad
-方法中使用CGRectZero框架初始化页面,并将其添加为子视图,然后稍后设置适当的框架,即:
在视图加载中
:
self.pageOne = [[UIView alloc] initWithFrame: CGRectZero];
self.pageOne.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.pageOne];
self.pageOne.frame = CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
ViewDidLayoutSubView中的:
self.pageOne = [[UIView alloc] initWithFrame: CGRectZero];
self.pageOne.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.pageOne];
self.pageOne.frame = CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
您必须等到实际布局完成后才能抓取帧,因此viewdilayoutsubviews
是一个不错的选择。如果要确保只添加一次页面,只需检查页面是否已经初始化,如下所示:
if (!self.pageOne) {
self.pageOne = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height)];
self.pageOne.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.pageOne];
}
当然,您也可以在viewDidLoad
-方法中使用CGRectZero框架初始化页面,并将其添加为子视图,然后稍后设置适当的框架,即:
在视图加载中
:
self.pageOne = [[UIView alloc] initWithFrame: CGRectZero];
self.pageOne.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.pageOne];
self.pageOne.frame = CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
ViewDidLayoutSubView中的:
self.pageOne = [[UIView alloc] initWithFrame: CGRectZero];
self.pageOne.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.pageOne];
self.pageOne.frame = CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
第一个解决方案不起作用,因为viewDidLayoutSubviews被调用了几次,而第一次调用它时,不能保证self.scrollView.bounds有其最终值,事实上,在我的情况下它没有,因此第一个解决方案会将self.pageOne帧修复为不正确的值。但第二个解决方案奏效了。谢谢。第一个解决方案不起作用,因为viewDidLayoutSubviews被调用了好几次,而且第一次调用它时,不能保证self.scrollView.bounds有它的最终值,事实上在我的情况下它没有,因此第一个解决方案会将self.pageOne框架修复为不正确的值。但第二个解决方案奏效了。谢谢