How to Make Outlet Connection to a Constraint in Ib

Connecting multiple constraints to a single outlet

Have you heard about IBOutletCollection? When you link any object from IB to VC code, in popup window you can select IBOutletCollection Option.

Tell me if it is not useful or if more info needed

Setting Auto Layout constraints of IBOutlet in code

If a storyboard or NIB has auto layout enabled for it, Xcode will not allow a view to lack constraints. Select the view and bring up the Size inspector. You will see a note to the effect that "The selected views have no constraints. At build time, explicit left, top, width, and height constraints will be generated for the view."

In general, Xcode will provide constraints to eliminate ambiguity in the layout.

You can work around this by adding sufficient constraints to the view but marking them as placeholders (remove at build time). This will leave the view without constraints.

You could also add constraints, set up outlets to the constraints, and remove them programmatically before adding other constraints.

Could not insert new outlet connection: Could not find any information for the class named

Here are some things that can fix this (in increasing order of difficulty):

  • Clean the project (Product > Clean)
  • Manually paste in

    @IBOutlet weak var viewName: UIView!
    // or
    @IBAction func viewTapped(_ sender: Any) { }

    and control drag to it. (Change type as needed.) Also see this.

  • Completely close Xcode and restart your project.

  • Delete the Derived Data folder (Go to Xcode > Preferences > Locations and click the gray arrow by the Derived Data folder. Then delete your project folder.)
  • Click delete on the class, remove reference (not Move to Trash), and add it back again. (see this answer)

Can't change Constraint IBOutlet that is defined for different size classes in IB

The problem is that constraints are not fully defined yet until Layout events happen between -viewWillLayoutSubviews and -viewDidLayoutSubviews where all the parameters from IB comes into play.
My rule of thumb is:

  • if you use frames to position your views manually you can do it as early as -viewDidLoad,
  • if you use autolayout constraints for positioning, make adjustments as early as -viewDidLayoutSubviews;

The second statements only considers code adjustments to constraints that have been made in IB. Adjustments that you are making in -viewDidLoad will be overridden by parameters set in IB during layout. If you add constraints with code you can set them in -viewDidLoad, since there will be nothing to override them.

I've changed your code a bit and it works:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topVerticalConstraint;
@property (weak, nonatomic) IBOutlet UIView *square;

@property (assign, nonatomic) BOOL firstLayout;

@end

@implementation ViewController

- (void)viewDidLoad

{
[super viewDidLoad];

self.firstLayout = YES;
}

- (void)viewDidLayoutSubviews {

[super viewDidLayoutSubviews];

if (self.firstLayout) {

self.topVerticalConstraint.constant = 10;
self.firstLayout = NO;

}
}

@end

Notice that -viewDidLayoutSubviews is called many times during the lifetime of a ViewController, so you have to make sure that your adjustments happen only once on initial load.

Created the UIButton using Interface Builder. Now i want to change the position and size of that button using Constraints

Button constraints

You can set the position from xib itself.

I have attached the image below there you can set width, height, leading , trailing, top and bottom constraint as you want.

Image for button Constraint

Here you can see Add New Constraints option here you can position the button as you want

Make height constraint outlet:

@IBOutlet var heightConst: NSLayoutConstraint!

Now In view did load set height as per device

if (UIDevice.current.screenType == .iPhones_5_5s_5c_SE){
heightConst.constant = 400
}else{
heightConst.constant = 200
}

Add UIDevice extension to your code (You can also find this at this link : how to check screen size of iphone 4 and iphone 5 programmatically in swift):

 extension UIDevice {
var iPhoneX: Bool {
return UIScreen.main.nativeBounds.height == 2436
}
var iPhone: Bool {
return UIDevice.current.userInterfaceIdiom == .phone
}
enum ScreenType: String {
case iPhones_4_4S = "iPhone 4 or iPhone 4S"
case iPhones_5_5s_5c_SE = "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
case iPhones_6_6s_7_8 = "iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
case iPhones_6Plus_6sPlus_7Plus_8Plus = "iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
case iPhones_X_XS = "iPhone X or iPhone XS"
case iPhone_XR = "iPhone XR"
case iPhone_XSMax = "iPhone XS Max"
case unknown
}
var screenType: ScreenType {
switch UIScreen.main.nativeBounds.height {
case 960:
return .iPhones_4_4S
case 1136:
return .iPhones_5_5s_5c_SE
case 1334:
return .iPhones_6_6s_7_8
case 1792:
return .iPhone_XR
case 1920, 2208:
return .iPhones_6Plus_6sPlus_7Plus_8Plus
case 2436:
return .iPhones_X_XS
case 2688:
return .iPhone_XSMax
default:
return .unknown
}
}
}

Can't link to outlet collection in XCode9

I had the same issue. This is the only workaround I could find to get it working is to create the Outlet Collection in code (or create the first one by dragging from the storyboard as you're doing). Then drag from the 'add' icon in the ViewController's line margin back to the objects on the storyboard rather than the usual way around.

Drag from the ViewController to the Storyboard

This also seems to be an alternative way:
https://stackoverflow.com/a/45597939/1320134

In summary, you need create the first outlet collection as you are currently doing, then drag from the newly created 'referencing outlet collection' in the Connections Inspector to the other objects you want to add to the collection in the storyboard.

How to update the constant height constraint of a UIView programmatically?

Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code.

yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()

Method updateConstraints() is an instance method of UIView. It is helpful when you are setting the constraints programmatically. It updates constraints for the view. For more detail click here.



Related Topics



Leave a reply



Submit