xcode 如何在nib中轻松设计UIScrollView的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9987436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to design the content of a UIScrollView in a nib easily
提问by Tibidabo
I have a scrollview which has to display a view larger than the available display area. I want to easily design the user interface without moving the embedded view up and down every time I have to do some changes. The problem is everything outside the visible area is invisible in IB.
我有一个滚动视图,它必须显示大于可用显示区域的视图。我想轻松地设计用户界面,而无需每次必须进行一些更改时上下移动嵌入式视图。问题是可见区域之外的所有东西在 IB 中都是不可见的。
Is there any switch or trick to make everything visible in IB?
是否有任何开关或技巧可以使 IB 中的所有内容都可见?
回答by rob mayoff
UPDATE
更新
I have posted another solution herewhich I think is simpler and better, and works in storyboards.
我在这里发布了另一个解决方案,我认为它更简单更好,并且适用于故事板。
ORIGINAL
原来的
Create your scroll view in the nib with the appropriate superview, position, and size.
使用适当的超级视图、位置和大小在笔尖中创建滚动视图。
Next, create a completely separate, top-levelUIView
instance by dragging a UIView
out of the palette and dropping it into the work area outside of any existing views. In the Attributes inspector, set the Size popup to “None” and make sure the Status Bar, Top Bar, and Bottom Bar are all set to None. Here's an example:
接下来,通过将一个拖出调色板并将其放入任何现有视图之外的工作区来创建一个完全独立的顶级UIView
实例UIView
。在属性检查器中,将大小弹出窗口设置为“无”,并确保状态栏、顶部栏和底部栏都设置为无。下面是一个例子:
This new top-level view will be your content view. Give your view controller two outlets: scrollView
and contentView
:
这个新的顶级视图将是您的内容视图。给你的视图控制器两个出口:scrollView
和contentView
:
@interface MyViewController
@property (nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIView *contentView;
@end
In the nib, wire up the scrollView
outlet to the scroll view and wire up the contentView
outlet to the content view.
在笔尖中,将scrollView
插座连接到滚动视图并将contentView
插座连接到内容视图。
Build your content view hierarchy inside the content view. Set its size as large as you need - it can be larger than 320x480 (as long as you have set all of its bars to None).
在内容视图中构建您的内容视图层次结构。根据需要将其大小设置为大 - 它可以大于 320x480(只要您将其所有条形设置为无)。
In your view controller's viewDidLoad
, add contentView
as a subview of scrollView
and set scrollView.contentSize
to the size of contentView
:
在您的视图控制器的 中viewDidLoad
,contentView
作为 的子视图添加scrollView
并设置scrollView.contentSize
为 的大小contentView
:
@implementation MyViewController
@synthesize scrollView = _scrollView;
@synthesize contentView = _contentView;
- (void)viewDidLoad {
[super viewDidLoad];
[self configureScrollView];
}
- (void)configureScrollView {
CGSize size = self.contentView.bounds.size;
self.contentView.frame = CGRectMake(0, 0, size.width, size.height);
[self.scrollView addSubview:self.contentView];
self.scrollView.contentSize = size;
// If you don't use self.contentView anywhere else, clear it here.
self.contentView = nil;
// If you use it elsewhere, clear it in `dealloc` and `viewDidUnload`.
}