Reputation: 1953
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
Reputation: 5906
You could edit the underlying XML. To do this:
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">
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 entirelyUpvotes: 1
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:
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
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
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