Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraint IBOutlets #6

Open
Codewaves opened this issue Feb 12, 2014 · 6 comments
Open

Constraint IBOutlets #6

Codewaves opened this issue Feb 12, 2014 · 6 comments

Comments

@Codewaves
Copy link

If view class object contains autolayout constraint IBOutlets, they will be set to nil after ARC deallocs nib and initial container view. Quick fix will be something like this:

        NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:firstItem attribute:constraint.firstAttribute relatedBy:constraint.relation toItem:secondItem attribute:constraint.secondAttribute multiplier:constraint.multiplier constant:constraint.constant];
        [self addConstraint:newConstraint];

        // Reconnect outlets
        @autoreleasepool
        {
           unsigned int numberOfProperties = 0;
           objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties);

           for (NSUInteger i = 0; i < numberOfProperties; i++)
           {
              objc_property_t property = propertyArray[i];
              NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];
              id        value = [self valueForKey:name];
              if (value == constraint)
                 [self setValue:newConstraint forKey:name];
           }
           free(propertyArray);
        }
@n-b
Copy link
Owner

n-b commented Feb 18, 2014

I'm not entirely sure, but hasn't #2 already solved the problem with autolayout in the container ?

@meknil
Copy link

meknil commented Oct 21, 2014

Hi, I did face the same issue regarding "constraint IBOutlets" today. The solution provided by Codewaves works well. I forked your repository to add the code snipped together with an example view. Are you interested??

@n-b
Copy link
Owner

n-b commented Oct 22, 2014

Maybe. Given #2, constraints should be ported to the new view. Why/when doesn't it work?

@meknil
Copy link

meknil commented Oct 22, 2014

Yes, the constraints are ported by making a hard copy! If I have a IBOutlet for a constraint (not view) defined then this outlet points to the original constraint. The suggested code above reconnects the defined outlet to the copy of the original constraint.

I use IBOutlet for a constraint to modify its constant property at runtime (or design time).
See my example here.

@n-b
Copy link
Owner

n-b commented Oct 22, 2014

Oh, OK. I'd like a solution that doesn't use the objc runtime, will look into it tomorrow. Thanks for the sample code.

@n-b
Copy link
Owner

n-b commented Oct 23, 2014

@meknil See pull request #9. I’d like your feedback on this, as I’m probably not going to use it anytime soon.

Thanks again for the example. I hadn’t had the opportunity to try IB_DESIGNABLE and �����IBInspectable, it makes NibLoadedView all the more interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants