简体   繁体   中英

iOS - referencing and programmatically moving a view from an XIB file

I'm using Xcode 8 and working on a legacy .xib file and need to figure out how to programmatically move all the objects. Questions:

  1. How can I programmatically reference the view (pointed to by the red arrow)?
  2. Assuming I can reference the view, would programmatically moving it also relatively move the four buttons and label contained within it?

Here's a screen shot of the View hierarchy of the .xib file:

在此处输入图片说明

Normally, I would just create a UIView object in a separate .xib file with the contents of the view from question #1, and add it the .xib file referenced above but this is existing code and I want to minimize changes if possible.

Assuming you have a view controller for this story board, Option+click your source file to open it in the assistant editor. You CTRL+Drag from the document outline (where your arrow is pointing) to the spot in your source file where you want the outlet to appear and Xcode will create an IBOutlet for the view.

If your question is can you load just the view from the story board without the view controller. I suppose you could instantiate the view controller, take a strong reference to the view, remove the view from its superview and then trash the viewcontroller, but that seems a bit wasteful; in this instance I would copy and paste the view into its own nib.

Note that as far as moving the view, yes it will move all of its subviews. If this is a temporary animation kind of thing consider using the .transform property along with one of the UIView Animation methods. You can also CTRL+Drag the constraints from interface builder and manipulate the constraints in code. You should avoid setting the frame/center manually if you are using constraints otherwise autolayout will be fighting with you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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