Skip to content

Instantly share code, notes, and snippets.

@ks-simakov
Created August 5, 2014 03:40
Show Gist options
  • Save ks-simakov/c22f06a772eef6c99ac1 to your computer and use it in GitHub Desktop.
Save ks-simakov/c22f06a772eef6c99ac1 to your computer and use it in GitHub Desktop.
Working with NSLayoutConstraint
//
// UIView+Constraints.h
//
// Created by Konstantin Simakov
//
#import <Foundation/Foundation.h>
@interface UIView (Constraints)
- (NSLayoutConstraint *)constraintByAttribute:(NSLayoutAttribute)attribute;
- (NSLayoutConstraint *)constraintByAttribute:(NSLayoutAttribute)attribute toView:(UIView *)view;
@end
//
// UIView+Constraints.m
//
// Created by Konstantin Simakov
//
#import "UIView+Constraints.h"
@implementation UIView (Constraints)
- (NSLayoutConstraint *)constraintByAttribute:(NSLayoutAttribute)attribute
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", attribute];
NSArray *filteredArray = [self.constraints filteredArrayUsingPredicate:predicate];
if (filteredArray.count == 0) {
return nil;
}
NSLayoutConstraint *constraint = [filteredArray objectAtIndex:0];
return constraint;
}
- (NSLayoutConstraint *)constraintByAttribute:(NSLayoutAttribute)attribute toView:(UIView *)view
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", attribute];
NSArray *filteredArray = [self.constraints filteredArrayUsingPredicate:predicate];
predicate = [NSPredicate predicateWithFormat:@"firstItem = %@", view];
NSArray *secondFilteredArray = [filteredArray filteredArrayUsingPredicate:predicate];
if (secondFilteredArray.count == 0) {
predicate = [NSPredicate predicateWithFormat:@"secondItem = %@", view];
secondFilteredArray = [filteredArray filteredArrayUsingPredicate:predicate];
}
if (secondFilteredArray.count == 0) {
return nil;
}
NSLayoutConstraint *constraint = [secondFilteredArray objectAtIndex:0];
return constraint;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment