6

When I move controls in Interface Builder and pass over a UIView. Is there anyway to stop IB from embedding the control in the UIView and making it a child of the UIView in the tree hierarchy.

BEFORE
+UIView
+UIButton
+UIView

If I move the UIButton with the mouse and place it above the UIView IB will make it a child of the UIView

AFTER
+UIView
  +UIButton
+UIView

Is there anyway to lock the bottom view. I often use them as backgrounds and tint them.

2
  • You can drag it to "Objects" list and then move by arrows.
    – folex
    Jan 9, 2013 at 12:02
  • Can you really? Doesn't for me (Xcode 5).
    – Benjohn
    May 20, 2014 at 14:43

2 Answers 2

4

You can reposition without embedding by pressing and holding Cmd while dragging the element.

Edit: This is only confirmed to work with Xcode 7.

5
  • If I hold cmd, I can't reposition the view at all. Aug 25, 2015 at 11:39
  • 1
    You need to start dragging the element before you press Cmd. I had this issue and filed a bug with Apple, where they suggested this solution.
    – Henrik
    Aug 25, 2015 at 13:33
  • It still doesn't work either way. (Xcode 6.4 (6E35b)) Aug 25, 2015 at 14:49
  • I have only tested this with the Xcode 7 betas, so maybe support for this is new.
    – Henrik
    Aug 25, 2015 at 18:28
  • Ok, please add this information to the answer so I can remove my downvote. Aug 26, 2015 at 8:05
0

I gave up and decided all groups of controls should have a parent UIView. Can be invisible (backgroundColor of clearColor)

Select all the controls then choose Editor menu > Embed in View.

After that we can safely move them around within their own grouping view without them jumping levels in the hierarchy.

One issue with Embed in View is when you use tagging and viewWithTag:

If you build your view using more than one VC controller and tag subviews make sure the tag ids are unique across the whole view hierarchy.

This is because viewWithTag: will only return the first control that matches the id and seems to search ACROSS the view hierachy before moving down a level to continue the search.

So if you choose Editor menu > Embed in View you're moving the control down a step in the hierarchy. So a call to viewWithTag:999 might have picked up your control before but now may return a completely different control.

It also may crash! Its common to cast the result from UIView to control e.g UIImageView Then call method on the control. If viewWithTag find a different control than the one expected it may not even be a UIImageView so calling a missing method would throw exception.

BASICALLY never use tags as you have to debug in XIB and code and no checking for duplicate ids. Drag from XIB to .h and create outlets instead.

1
  • Nope, I still get the same problem – the view my what ever is embedded in now jumps to be a child of something it should be overlapping.
    – Benjohn
    May 20, 2014 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.