基于在 Storyboard 中使用自动布局创建的另一个视图,以编程方式使用框架创建视图
Creating a view programmatically with frame based on another view created in Storyboard with Auto-layout
我在情节提要中添加了一个滚动视图,并在情节提要中设置了它的自动布局属性。很明显,不同的 iPhone 需要不同的尺寸,这正是我想要的。现在我想在滚动视图中添加两个子视图,每个都占据滚动视图的整个可见区域并启用分页。因此,在任何时间点,其中一个子视图是可见的,您可以向左或向右滑动以查看另一个子视图。
我的问题是,由于我是在程序中创建这两个视图,所以我不确定应该在哪里设置子视图框架。这就是我现在在 viewDidLayoutSubviews 中所做的,但理想情况下我想在 viewDidload 中执行此操作,但在 viewDidLoad 中尚未设置框架:
@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);
}
所以,我的问题是,如果我在程序中创建的视图(上面的 pageOne 和 pageTwo)指的是 Storyboard 中设置的视图的框架,并为其大小自动布局,我应该在哪里设置代码。我知道我的代码存在多次执行的问题,这不是我想要的。
需要等到实际布局完成后才能抓取框架,所以viewDidLayoutSubviews
是个不错的选择。如果你想确保页面只被添加一次,你可以简单地包括一个检查它们是否已经被初始化,像这样:
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];
}
当然,您也可以使用 CGRectZero 框架在 viewDidLoad
方法中初始化页面并将它们添加为子视图,然后稍后设置适当的框架,即:
在viewDidLoad
中:
self.pageOne = [[UIView alloc] initWithFrame: CGRectZero];
self.pageOne.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.pageOne];
在viewDidLayoutSubviews
中:
self.pageOne.frame = CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
我在情节提要中添加了一个滚动视图,并在情节提要中设置了它的自动布局属性。很明显,不同的 iPhone 需要不同的尺寸,这正是我想要的。现在我想在滚动视图中添加两个子视图,每个都占据滚动视图的整个可见区域并启用分页。因此,在任何时间点,其中一个子视图是可见的,您可以向左或向右滑动以查看另一个子视图。
我的问题是,由于我是在程序中创建这两个视图,所以我不确定应该在哪里设置子视图框架。这就是我现在在 viewDidLayoutSubviews 中所做的,但理想情况下我想在 viewDidload 中执行此操作,但在 viewDidLoad 中尚未设置框架:
@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);
}
所以,我的问题是,如果我在程序中创建的视图(上面的 pageOne 和 pageTwo)指的是 Storyboard 中设置的视图的框架,并为其大小自动布局,我应该在哪里设置代码。我知道我的代码存在多次执行的问题,这不是我想要的。
需要等到实际布局完成后才能抓取框架,所以viewDidLayoutSubviews
是个不错的选择。如果你想确保页面只被添加一次,你可以简单地包括一个检查它们是否已经被初始化,像这样:
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];
}
当然,您也可以使用 CGRectZero 框架在 viewDidLoad
方法中初始化页面并将它们添加为子视图,然后稍后设置适当的框架,即:
在viewDidLoad
中:
self.pageOne = [[UIView alloc] initWithFrame: CGRectZero];
self.pageOne.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.pageOne];
在viewDidLayoutSubviews
中:
self.pageOne.frame = CGRectMake(0, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);