xcode 获取现有的 NSLayoutConstraint 宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13857115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting an existing NSLayoutConstraint for the width?
提问by IluTov
I'm trying to animate a control in Cocoa with auto layout.
我正在尝试使用自动布局为 Cocoa 中的控件设置动画。
Now, I can set [[constraint animator] setConstant:newWidth];
, which works.
But how can I get the right constraint?
现在,我可以设置[[constraint animator] setConstant:newWidth];
,这很有效。但是我怎样才能得到正确的约束呢?
With [self constraints]
you can get all the constraints, and in this case I can just select constraints[0]
, but the order of the constraints may vary.
有了[self constraints]
你可以得到所有的约束,在这种情况下我可以选择constraints[0]
,但限制的顺序可能会有所不同。
How can I be certain I always have the right constraint? The constraints are set in Interface Builder. I have seen that you can add a IBOutlet to it, but it doesn't seem necessary.
我怎么能确定我总是有正确的约束?约束在 Interface Builder 中设置。我已经看到您可以向其添加 IBOutlet,但似乎没有必要。
My solution
我的解决方案
Thanks, it worked great. I wrote a little category.
谢谢,效果很好。我写了一个小类。
NSView+NSLayoutConstraintFilter.h
NSView+NSLayoutConstraintFilter.h
#import <Cocoa/Cocoa.h>
@interface NSView (NSLayoutConstraintFilter)
- (NSLayoutConstraint *)constraintForAttribute:(NSLayoutAttribute)attribute;
- (NSArray *)constaintsForAttribute:(NSLayoutAttribute)attribute;
@end
NSView+NSLayoutConstraintFilter.m
NSView+NSLayoutConstraintFilter.m
#import "NSView+NSLayoutConstraintFilter.h"
@implementation NSView (NSLayoutConstraintFilter)
- (NSArray *)constaintsForAttribute:(NSLayoutAttribute)attribute {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", attribute];
NSArray *filteredArray = [[self constraints] filteredArrayUsingPredicate:predicate];
return filteredArray;
}
- (NSLayoutConstraint *)constraintForAttribute:(NSLayoutAttribute)attribute {
NSArray *constraints = [self constaintsForAttribute:attribute];
if (constraints.count) {
return constraints[0];
}
return nil;
}
@end
回答by Sunil Pandey
Every contraint has an attribute [constraint firstAttribute]
It returns an enum NSLayoutAttribute
每个[constraint firstAttribute]
约束都有一个属性它返回一个枚举 NSLayoutAttribute
typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
NSLayoutAttributeLeft = 1,
NSLayoutAttributeRight,
NSLayoutAttributeTop,
NSLayoutAttributeBottom,
NSLayoutAttributeLeading,
NSLayoutAttributeTrailing,
NSLayoutAttributeWidth,
NSLayoutAttributeHeight,
NSLayoutAttributeCenterX,
NSLayoutAttributeCenterY,
NSLayoutAttributeBaseline,
NSLayoutAttributeNotAnAttribute = 0
};
so you can check NSLayoutAttributeWidth for width.
所以你可以检查 NSLayoutAttributeWidth 的宽度。
Sample code:
示例代码:
NSArray constraints = [self constraints];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", NSLayoutAttributeWidth];
NSArray *filteredArray = [constraints filteredArrayUsingPredicate:predicate];
if(filteredArray.count == 0){
return nil;
}
NSLayoutConstraint *constraint = [constraints objectAtIndex:0];
回答by longkai
Here is the swift 3 version tested on Xcode 8.2.1 and macOS 10.12.2.
这是在 Xcode 8.2.1 和 macOS 10.12.2 上测试的 swift 3 版本。
The code shows how to get a button's width and height constraints, but you could filter whatever you want from NSLayoutAttribute
enum.
代码显示了如何获取按钮的宽度和高度约束,但您可以从NSLayoutAttribute
枚举中过滤任何您想要的内容。
let cons = signInButton.constraints.filter {
/**
* Utils when working with constraints
*/
extension NSLayoutConstraint{
/**
* Returns all constraints of kinds
* EXAMPLE: NSLayoutConstraint.ofKind(rect.immediateConstraints, kinds: [.width,.height]) //width, height
*/
static func ofKind(_ constraints:[NSLayoutConstraint],kinds:[NSLayoutAttribute]) -> [NSLayoutConstraint]{
return kinds.map { kind in
return constraints.filter { constraint in
return constraint.firstAttribute == kind
}
}.flatMap({##代码##})//flattens 2d array to 1d array
}
}
.firstAttribute == NSLayoutAttribute.width || ##代码##.firstAttribute == NSLayoutAttribute.height /// or other from `NSLayoutAttribute`
}
// do something with the constraints array, e.g.
NSLayoutConstraint.deactivate(cons)