简体   繁体   中英

In Xcode, how can you rename a constraint in a Storyboard?

I'm working on a Storyboard in Xcode, and I added a Label to a view controller I'm working on. Since I try to make all of my apps use Auto-Layout, my first reaction is to add Auto-Layout constraints to the Label. So I add 2 constraints to the Top and Left edges of the superview the Label is in. And I also add constraints on the Label itself for Width and Height, and I know that I can modify the value of the constraints afterwards to set them to what I want them to be. My Storyboard looks like this:

具有4个约束的标签

Then I realized that I forgot to change the name of the Label to something more descriptive. Since this is the Title label for my main view, I renamed the Label to "Title" and also renamed the top level View to "MainView". So now my Storyboard looks like this:

MainView中具有约束的标题

Then I realized that my constraints still reference "Label" and "View", so I go to rename them. I highlighted the constraint for Width, pressed enter and got an edit text field and replaced the word "Label" with "Title" and hit enter again. The edit text field closed, but the constraint name went back to being "Label" instead of "Title" like I typed. I tried again, same result. I tried with the Height constraint, same result. It wouldn't let me change the name of the constraint. So I deleted the Width constraint and re-made a Width constraint. Now it has the right name referencing "Title" instead of "Label". But I don't want to have to delete the constraint and recreate it every time I change the name of a UI element the constraint is tied to.

Is there any way to rename a constraint after I change the name of an element that the constraint is tied to?

Since this had been plaguing me in several apps recently, I did a lot of searching trying to find a way to do this. I opened the Storyboard file in a text editor to search for the text, but it wasn't there. I looked around stack overflow, but couldn't find anything about renaming storyboard constraints. Then I stumbled onto a solution. Here's my solution.

Click on the constraint you want to change (do the Width one first, but it really doesn't matter). In the Utilities right-hand section, under the 3rd option "Identity Inspector", in the "Document" section there is a "Label" text box with placeholder text saying "Xcode Specific Label". If you press enter on the constraint, you can highlight the existing name and copy it. Then go over to the Label text box in the Identity Inspector and paste it in there. You can change the word "Label" to "Title" to fit the new name and press enter. You'll see the constraint name change in the Storyboard hierarchy.

But you're not quite done! Let's say you wanted to change the text of the Title to your specific title name: "My Specific Title". Well, when you do, the label won't be wide enough to show all that text, so you go and update your width constraint to be 140 and now you can see the whole title. But your constraint still says "Width - (42) - Title", when you want it to read "Width - (140) - Title" because you just updated the Width to 140.

It turns out that while you have any text in that "Label" field in the Identity Inspector, it will always say whatever that text is. But after you initially change the name to say "Width - (42) - Title" to match the name of your Label and press enter or click in another field (anything that will get it to accept the text change), you can then delete the contents of the Label textbox and your constraint will still reference the new name and then automatically update as you change the constraint. So if you changed the Width constraint to be 140, it will now read "Width - (140) - Title".

So after you change the name of an element and you want to change the name of the constraints as well, just copy and paste the current constraint name into that Label text box, press enter to get it to make the change, then delete it from the Label text box.

Note that the only reason you couldn't change the constraint name was because the element's name was changed first. If you change the text in the constraint while it still matches the element referenced in the constraint, then it will let you change it and it will automatically put the new name in that Label textbox in the Identity Inspector. Then you could change the name of the element to match the name you entered in the constraint name. But because you have text in that Label textbox, the constraint will ALWAYS display that. So after you have the element name matching the new text in the constraint name, I recommend deleting the text from the Label textbox so that the constraint name will auto-update when you change the constraint.

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