Created
August 5, 2014 03:40
-
-
Save ks-simakov/c22f06a772eef6c99ac1 to your computer and use it in GitHub Desktop.
Working with NSLayoutConstraint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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