Rus*_*sik 9 uikit ios autolayout
我想在我的iOS应用程序中使用-frameForAlignmentRect:和-alignmentRectForFrame:指定对齐方式.但这种方法没有调用.
我有几乎相同的iOS和Mac代码.它在OS X上完美运行,但在iOS上运行不正常.
@implementation CustomView
#define SHADOW_SIZE 10
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *path;
[[NSColor redColor] set];
path = [NSBezierPath bezierPathWithRect:dirtyRect];
[path fill];
// Draw a shadow
NSRect rect = NSMakeRect(SHADOW_SIZE, 0, dirtyRect.size.width - SHADOW_SIZE, dirtyRect.size.height - SHADOW_SIZE);
path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:32 yRadius:32];
[[[NSColor blackColor] colorWithAlphaComponent:0.3f] set];
[path fill];
// Draw shape
rect.origin = CGPointMake(0, SHADOW_SIZE);
path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:32 yRadius:32];
[[NSColor orangeColor] set];
[path fill];
}
#define USE_ALIGNMENT_RECTS 1
#if USE_ALIGNMENT_RECTS
- (NSRect)frameForAlignmentRect:(NSRect)alignmentRect {
NSRect rect = alignmentRect;
rect.origin.y -= SHADOW_SIZE;
rect.size.width += SHADOW_SIZE;
rect.size.height += SHADOW_SIZE;
return rect;
}
- (NSRect)alignmentRectForFrame:(NSRect)frame {
NSRect rect;
rect.origin = CGPointMake(frame.origin.x, frame.origin.y + SHADOW_SIZE);
rect.size.width = frame.size.width - SHADOW_SIZE;
rect.size.height = frame.size.height - SHADOW_SIZE;
return rect;
}
#endif
@end
@implementation AppDelegate
- (void)awakeFromNib {
_view = _window.contentView;
CustomView *view = [[CustomView alloc] init];
[self.view addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
// Set vertical and horizontal alignments and view size
// A little trick from here https://github.com/evgenyneu/center-vfl
NSDictionary *views = NSDictionaryOfVariableBindings(_view, view);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_view]-(<=1)-[view(200)]"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_view]-(<=1)-[view(200)]"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:views]];
}
@end
Run Code Online (Sandbox Code Playgroud)
@implementation MyView
#define SHADOW_SIZE 10
- (void)drawRect:(CGRect)dirtyRect {
UIBezierPath *path;
[[UIColor redColor] set];
path = [UIBezierPath bezierPathWithRect:dirtyRect];
[path fill];
// Draw a shadow
CGRect rect = CGRectMake(SHADOW_SIZE, SHADOW_SIZE, dirtyRect.size.width - SHADOW_SIZE, dirtyRect.size.height - SHADOW_SIZE);
path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:32];
[[[UIColor blackColor] colorWithAlphaComponent:0.3f] set];
[path fill];
// Draw shape
rect.origin = CGPointMake(0, 0);
path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:32];
[[UIColor orangeColor] set];
[path fill];
}
#define USE_ALIGNMENT_RECTS 1
#define USE_ALIGNMENT_INSETS 1
#if USE_ALIGNMENT_RECTS
- (CGRect)frameForAlignmentRect:(CGRect)alignmentRect {
CGRect rect = alignmentRect;
rect.size.width += SHADOW_SIZE;
rect.size.height += SHADOW_SIZE;
return rect;
}
- (CGRect)alignmentRectForFrame:(CGRect)frame {
CGRect rect = frame;
rect.size.width = frame.size.width - SHADOW_SIZE;
rect.size.height = frame.size.height - SHADOW_SIZE;
return rect;
}
#endif
#if USE_ALIGNMENT_INSETS
- (UIEdgeInsets)alignmentRectInsets {
return UIEdgeInsetsMake(0, 0, SHADOW_SIZE, SHADOW_SIZE);
}
#endif
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyView *view = [[MyView alloc] init];
[self.view addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
UIView *superview = self.view;
NSDictionary *views = NSDictionaryOfVariableBindings(superview, view);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[view(200)]"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[view(200)]"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:views]];
}
@end
Run Code Online (Sandbox Code Playgroud)
你可以看到iOS版本也-alignmentRectInsets:运行良好,但我想弄清楚什么是错误的-frameForAlignmentRect:和-alignmentRectForFrame:方法.
| 归档时间: |
|
| 查看次数: |
1278 次 |
| 最近记录: |