如何在Interface Builder中为Storyboard设置Target专属值?
Absolutely! You can set target-specific constraint multipliers right in Interface Builder—no code required, which is perfect for launch screens since you can’t modify them programmatically. Here’s a straightforward, step-by-step solution:
Method 1: Conditional Constraint Installation (Most Reliable)
This approach uses Interface Builder’s built-in variation support to activate only the correct constraint for each target:
Step 1: Add duplicate constraints to your logo
Create two identical constraint types (e.g., aspect ratio, width relative to superview) for your logo image view. Set each constraint to the multiplier value you need for your two targets (e.g.,1.0for Target A,1.5for Target B).Step 2: Configure target-specific installation
For each constraint:- Select the constraint in the storyboard canvas or document outline.
- Open the Attributes Inspector (right sidebar, slider icon).
- Find the Installed checkbox. Click the small dropdown arrow next to it, then select Add Variation.
- A list of your project’s targets will appear. Check the box only for the target you want this constraint to apply to, and uncheck it for the other target.
Repeat this for the second constraint, checking the opposite target.
Step 3: Validate your setup
Switch between your targets using the scheme selector at the top of Xcode. When you select a target, only the constraint marked as "Installed" for that target will be active in the storyboard preview—you’ll see the logo adjust automatically to match the multiplier.
Why This Works
Launch screens are compiled into a static asset during build time, and Interface Builder will only include the constraints marked as installed for the active target. This means each target gets exactly the multiplier you want, no runtime code needed.
内容的提问来源于stack exchange,提问作者Oleg Shcherbakov

