Ios 使用自动布局将特定边框添加到UIView
在Swift中找到了解决方案: 在Objective-C中需要它。所以我可能需要挖掘Swift来转换它,但我还没有学会Swift Swift代码:Ios 使用自动布局将特定边框添加到UIView,ios,objective-c,Ios,Objective C,在Swift中找到了解决方案: 在Objective-C中需要它。所以我可能需要挖掘Swift来转换它,但我还没有学会Swift Swift代码: func addBorder(edges edges: UIRectEdge, colour: UIColor = UIColor.whiteColor(), thickness: CGFloat = 1) -> [UIView] { var borders = [UIView]() func border() ->
func addBorder(edges edges: UIRectEdge, colour: UIColor = UIColor.whiteColor(), thickness: CGFloat = 1) -> [UIView] {
var borders = [UIView]()
func border() -> UIView {
let border = UIView(frame: CGRectZero)
border.backgroundColor = colour
border.translatesAutoresizingMaskIntoConstraints = false
return border
}
if edges.contains(.Top) || edges.contains(.All) {
let top = border()
addSubview(top)
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("V:|-(0)-[top(==thickness)]",
options: [],
metrics: ["thickness": thickness],
views: ["top": top]))
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("H:|-(0)-[top]-(0)-|",
options: [],
metrics: nil,
views: ["top": top]))
borders.append(top)
}
if edges.contains(.Left) || edges.contains(.All) {
let left = border()
addSubview(left)
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("H:|-(0)-[left(==thickness)]",
options: [],
metrics: ["thickness": thickness],
views: ["left": left]))
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("V:|-(0)-[left]-(0)-|",
options: [],
metrics: nil,
views: ["left": left]))
borders.append(left)
}
if edges.contains(.Right) || edges.contains(.All) {
let right = border()
addSubview(right)
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("H:[right(==thickness)]-(0)-|",
options: [],
metrics: ["thickness": thickness],
views: ["right": right]))
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("V:|-(0)-[right]-(0)-|",
options: [],
metrics: nil,
views: ["right": right]))
borders.append(right)
}
if edges.contains(.Bottom) || edges.contains(.All) {
let bottom = border()
addSubview(bottom)
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("V:[bottom(==thickness)]-(0)-|",
options: [],
metrics: ["thickness": thickness],
views: ["bottom": bottom]))
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("H:|-(0)-[bottom]-(0)-|",
options: [],
metrics: nil,
views: ["bottom": bottom]))
borders.append(bottom)
}
return borders
}
p.S-我已要求原始回答者在Objective-C中提供,但他可能需要一些时间才能做出回应。我发布它的目的是,我只想在这里有一个更快的解决方案
问候 这应该行得通。在UIView上创建一个类别,然后可以这样使用它:
[self.scrollView addAutolayoutBorder:UIRectEdgeLeft | UIRectEdgeRight | UIRectEdgeBottom
colour:[UIColor redColor]
thickness:2.0f];
分类如下:
.h
#import <UIKit/UIKit.h>
@interface UIView (AutoLayoutBorder) {
}
- (NSMutableArray *)addAutolayoutBorder:(UIRectEdge)edges
colour:(UIColor *)colour
thickness:(CGFloat)thickness;
@end
您好,我今天尝试了您的代码很长一段时间后,这似乎不工作,虽然在这段代码中没有错误。我必须试着站在你这边。我是否需要创建实例来访问此方法?可能存在重复的
#import "UIView+AutoLayoutBorder.h"
@implementation UIView (AutoLayoutBorder)
- (NSMutableArray *)addAutolayoutBorder:(UIRectEdge)edges
colour:(UIColor *)colour
thickness:(CGFloat)thickness {
NSMutableArray *borderViewsArray = [NSMutableArray new];
UIView * (^getBorder)(void) = ^UIView *{
UIView *borderView = [UIView new];
borderView.translatesAutoresizingMaskIntoConstraints = NO;
borderView.backgroundColor = colour;
return borderView;
};
if ((edges & UIRectEdgeTop) == UIRectEdgeTop || (edges & UIRectEdgeAll) == UIRectEdgeAll) {
UIView *top = getBorder();
[self addSubview:top];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[top(==thickness)]"
options:0
metrics:@{@"thickness" : @(thickness)}
views:@{@"top" : top}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[top]-(0)-|"
options:0
metrics:nil
views:@{@"top" : top}]];
[borderViewsArray addObject:top];
}
if ((edges & UIRectEdgeLeft) == UIRectEdgeLeft || (edges & UIRectEdgeAll) == UIRectEdgeAll) {
UIView *left = getBorder();
[self addSubview:left];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[left(==thickness)]"
options:0
metrics:@{@"thickness" : @(thickness)}
views:@{@"left" : left}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[left]-(0)-|"
options:0
metrics:nil
views:@{@"left" : left}]];
[borderViewsArray addObject:left];
}
if ((edges & UIRectEdgeRight) == UIRectEdgeRight || (edges & UIRectEdgeAll) == UIRectEdgeAll) {
UIView *right = getBorder();
[self addSubview:right];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[right(==thickness)]-(0)-|"
options:0
metrics:@{@"thickness" : @(thickness)}
views:@{@"right" : right}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[right]-(0)-|"
options:0
metrics:nil
views:@{@"right" : right}]];
[borderViewsArray addObject:right];
}
if ((edges & UIRectEdgeBottom) == UIRectEdgeBottom || (edges & UIRectEdgeAll) == UIRectEdgeAll) {
UIView *bottom = getBorder();
[self addSubview:bottom];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bottom(==thickness)]-(0)-|"
options:0
metrics:@{@"thickness" : @(thickness)}
views:@{@"bottom" : bottom}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[bottom]-(0)-|"
options:0
metrics:nil
views:@{@"bottom" : bottom}]];
[borderViewsArray addObject:bottom];
}
return borderViewsArray;
}
@end