Xcode - How to Drag a Component from One View to Another Without Losing Its Frame

Xcode - Is there a way to drag a component from one view to another without losing its frame?

Another solution:

  1. Select the items you want in your subview, then
  2. (in the tool bar) Editor > Embed In > view type to embed in.

Moving subviews from one view to another view without changing any position information in storyboard?

Yes there is an easy way to do this:

Step 1: Select your views

enter image description here

Step 2: Go Editor > Embed In > Scroll View

enter image description here

Done!

enter image description here

PS. This is a very handy way to group views in fact. You can embed any views into a 'container' view, move it wherever you like (even cross-scene) retaining relative position information and then you could keep it as a group or unembed them.

How to copy a view in Interface Builder by Just Dragging Mouse?

You can duplicate views if you drag them with alt.

Preserve position when moving UIView in hierarchy (paste-in-place)

Was looking for the solution to the same annoying limitation in xCode... found a trick:

  • Create a new UIViewController in IB
  • Paste all your stuff in its root view, the positions should be preserved
  • Copy and paste this root view where ever you need it,

worked for me but geeesh what a pain.

Reposition view which is going out of screen frame

After calculating imageWidth and imageX you could make a check like this:

[...]
var imageX = myButton.minX -
((myView.myImageView.bounds.width)/2) + 15
imageX = min(imageX, view.frame.width - imageWidth)
[...]

Notice that i changed imageX from let to var.

Is it possible to drag a view out of the scene dock into its own XIB?

Of course you can, using Assistant Editor.

First, create a new empty .xib file. Then open Assistant Editor. On one side let the .xib file, and on the other storyboard. Then just drag and drop the scene dock.
Here's screenshot:

Drag and Drop

Is there a way to add subviews to Xcode without reseting the position of the last view added?

Because you set a constraints at custom view creating.

After custom view was added to superview, you change its position by dragging - but you do it just change center property. Ok.

When you did added new custom view to your superview - will be executed layout subviews. And all your subviews positions will be reseted according initial constraints.

You need:

  • Either do not use constraints in your subviews
  • Either In your handlePan(_ gestureRecognizer : UIPanGestureRecognizer) func change
    centerXAnchor,centerYAnchor constraint values, not center property. Because after any layout subviews, their positions will be set to original.

    let newView = UIImageView.init(frame: .init(x: 0, y: 0, width: 100, height: 100))
    newView.translatesAutoresizingMaskIntoConstraints = false
    newView.isUserInteractionEnabled = true
    newView.backgroundColor = UIColor.random
    view.addSubview(newView)

    // newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
    // newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
    // newView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    // newView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

    let panGR = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
    panGR.delegate = self
    newView.addGestureRecognizer(panGR)

This code works good.

Version with constraints:

@IBAction func addView() {
let newView = CustomView.init()
newView.translatesAutoresizingMaskIntoConstraints = false
newView.isUserInteractionEnabled = true
newView.backgroundColor = UIColor.random
view.addSubview(newView)

newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
newView.centerXConstraint = newView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
newView.centerYConstraint = newView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
newView.centerXConstraint.isActive = true
newView.centerYConstraint.isActive = true

let panGR = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
panGR.delegate = self
newView.addGestureRecognizer(panGR)
}

@objc func handlePan(_ gestureRecognizer : UIPanGestureRecognizer){
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed,
let view = gestureRecognizer.view as? CustomView,
let centerXConstraint = view.centerXConstraint,
let centerYConstraint = view.centerYConstraint {
let translation = gestureRecognizer.translation(in: self.view)
let x = centerXConstraint.constant + translation.x
let y = centerYConstraint.constant + translation.y
centerXConstraint.constant = x
centerYConstraint.constant = y
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}

Add constraint variables in your custom class

class CustomView: UIView {
var centerXConstraint: NSLayoutConstraint!
var centerYConstraint: NSLayoutConstraint!

}


Related Topics



Leave a reply



Submit