简体   繁体   English

将UIView从xib添加到UIScrollView

[英]Add UIView from xib to UIScrollView

I am making an application, and I started with a single view controller with one view inside. 我正在制作一个应用程序,我从一个带有一个视图的单视图控制器开始。 I made my app but I want to have multiple pages and allow the user to swipe between them. 我制作了我的应用,但我希望有多个页面,并允许用户在它们之间滑动。 I want to use a UISCrollView. 我想使用UISCrollView。 How can I set up a UIScrollView so that I can insert my already made view from the xib. 如何设置UIScrollView,以便可以从xib插入已经完成的视图。 I don't want to make a view programmatically because a lot of it is graphical and I don't really know what I'm doing. 我不想以编程方式创建视图,因为很多视图都是图形的,我真的不知道自己在做什么。

Recap: UIScrollView to swipe between views I have made in xib files 回顾:UIScrollView在Xib文件中创建的视图之间滑动

in your main view controller, add a scrollview. 在主视图控制器中,添加一个滚动视图。 You can add scrollview to mainview in Xib. 您可以在Xib中将scrollview添加到mainview中。

In mainview controller .h file add a property , then load programmatically your views and add to scroll. 在mainview控制器.h文件中,添加一个属性,然后以编程方式加载您的视图并进行滚动。 Set each view position. 设置每个视图位置。 Set scrollview reference in your main xib properly. 在主XIB中正确设置scrollview参考。

    @property(nonatomic, weak) IBoutlet UIScrollView *scrollView;

UIView *view1 = [[NSBundle mainBundle] loadNibNamed:@"myViewXib1" owner:self options:nil];
UIView *view2 = [[NSBundle mainBundle] loadNibNamed:@"myViewXib2" owner:self options:nil];
UIView *view3 = [[NSBundle mainBundle] loadNibNamed:@"myViewXib3" owner:self options:nil];

CGRect frame;
frame = view1.frame;
frame.origin.x = 0;
view1.frame = frame;

frame = view2.frame;
frame.origin.x = view1.frame.size.width + view1.frame.origin.x ;
view2.frame = frame;

frame = view3.frame;
frame.origin.x = view2.frame.size.width + view2.frame.origin.x;
view3.frame = frame;
[scrolView addSubView:view1];
[scrolView addSubView:view2];
[scrolView addSubView:view3];

Declare IBOutlet in your view controller to point to that view. 在视图控制器中声明IBOutlet指向该视图。

@property(nonatomic, weak) IBoutlet UIView *myView;

Connect your view from xib to myView. 将您的视图从xib连接到myView。

Load your xib in the view controller viewDidLoad method 将Xib加载到视图控制器的viewDidLoad方法中

[[NSBundle mainBundle] loadNibNamed:@"myViewXib" owner:self options:nil];

Then add myView to your scrollview. 然后将myView添加到您的滚动视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM