Erez
Erez

Reputation: 1953

Xcode changing object class after adding it to storyboard

I've added multiple elements to view controller and added complicated constraints to them. But now I want to change then from UIImageView to UIButton.

Is it posible to change a class in the storyboard?

I know about the class property in the identity inspector but that is not changing the element.

I really don't want to make the constraints again, but if there is no other way that is what i will have to do.

Upvotes: 0

Views: 1554

Answers (4)

sketchyTech
sketchyTech

Reputation: 5906

You could edit the underlying XML. To do this:

  1. make sure you've backed up the current storyboard under version control, or simply make a copy
  2. drag out the new item that you want onto the visual storyboard
  3. open storyboard as source code (right-click on the Storyboard to Open As > Source Code )

You will then see that constraints are listed like so:

<constraints>
    <constraint firstItem="NBy-b0-qdt" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="JOl-XX-NS8"/>
    <constraint firstItem="NBy-b0-qdt" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="mFA-R7-Xir"/>
</constraints>

The firstItem and secondItem relate to the ids of the objects so all you need to do is to swap out the firstItem (or secondItem) id for the id of the new object.

Note: your objects will be inside the subviews tag and will look something like this:

<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="NBy-b0-qdt">
  1. Once you've changed the firstItem (or secondItem) value within each of the relevant constraints to correspond to the correct id for your new object, the xml for the old item can be removed entirely
  2. You will need to now right-click on the Storyboard and Open As > Interface Builder - Storyboard
  3. Now ensure that all the links between the storyboard and the view controller code are updated with the correct class

Upvotes: 1

Erez
Erez

Reputation: 1953

Thank to every one,

I dont like the idea of being lazy and adding more elements just because, so I decided on the following approach and it worked pretty good for me:

  1. add all the buttons i need to the view, without nothing. just resized them to the right size.
  2. moved each image slightly up so there will be a space for the button.
  3. positioned each button in place of the image.
  4. the constraints where already there in front of me so it was just a matter of "copy and paste" (i meant it was easy to set them correctly).

p.s. there are dozens of constraints in this container and it took a very short time relatively to do it, i hope not making this mistake again but probably will eventually. for me it is much better then adding elements (recogniser or another button on top) and the code that comes with it

10x everyone.

Upvotes: 0

hybridcattt
hybridcattt

Reputation: 3041

I don't think it is possible to replace a view. But there's another solution.

Leave your UIImageView as it is, transparent, and use it as a container, adding your UIButton as a subview to it, and adding constraints so the button would fill up the whole image view.

Make your image view have transparent background, and don't forget to set userInteractionsEnabled to YES.

Compared to adding UITapGestureRecognizer, with this approach you will have an actual button with all features that come with it.

Upvotes: 2

Eduardo Oliveros
Eduardo Oliveros

Reputation: 857

its not posible, the better way: add a tap gesture to the UIImage, i suggest in the future use stack views and "Add missing Constraints", with this you don't have many troubles to change it in a future

Upvotes: 2

Related Questions