How to design separate UIView outside of any ViewC

2019-02-04 20:14发布

问题:

I would like to design a UIView, which is larger than a ViewController in Storyboard (iOS 5).

The UIView should be used as the subview of a UIScrollView and hence be larger than any of my existing ViewControllers. How can I create such a UIView in Storyboard and associate it with my UIScrollView?

I would like to do this without xib files if possible.

Thank you!

回答1:

I see no other option than using xibs, but it's not that annoying:

//We have file called "View.xib" in our project. It contains one SINGLE view
NSArray *xibContents = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil];
UIView *view = [xibContents lastObject]; //safer than objectAtIndex:0

[self.scrollview addSubview:view];
self.scrollview.contentSize = view.frame.size; 

In order to make IB connections you can set the filesOwner class in the xib to be your viewController, and connect like usual.



回答2:

You can place a UIView into your scrollview and directly design it inside the viewController of your scrollView



回答3:

I've found the way to edit the view added to the scene (being at the same hierarchy level as ViewController).

Unfortunately it is from hackish types of actions. My Xcode version is Version 4.5.2 (4G2008a). I've tested this in real project and new empty project.

The basic idea is that Xcode do have an ability to edit such external views, unfortunately this mode doesn't activate straightforwardly.

In the method that I've found you need to have 2 levels of hierarchy inside your external view:

Scene
|- VC
   |- View
|- ExternalView
   |- SubView1
      |- SubView2
  • Then goto Document Outline panel
  • find SubView2 in the tree of your scene
  • double click it

The editing area will appear and its coordinates will be saved to project's user data, so you can move it to more suitable place if you want to, and next time you'll open the storyboard in IB on the machine it will be there. Although I think on other machines you'll have to do it again (I haven't tested that).