gpt4 book ai didi

ios - 为什么-frameForAlignmentRect:和-alignmentRectForFrame:不调用IOS

转载 作者:可可西里 更新时间:2023-11-01 03:01:07 27 4
gpt4 key购买 nike

我想使用-frameForAlignmentRect:-alignmentRectForFrame:在我的ios应用程序中指定对齐矩形。但是这个方法没有调用。
我有几乎相同的iOS和Mac代码。它在OS X上运行得很好,但在iOS上却不行。
Mac版本

@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

iOS版本
@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

正如您所看到的,ios版本也有 -alignmentRectInsets:这一功能很好,但是我想找出 -frameForAlignmentRect:-alignmentRectForFrame:方法有什么问题。

最佳答案

您需要从以下位置更改代码:

view.translatesAutoresizingMaskIntoConstraints = NO;

到:
self.view.translatesAutoresizingMaskIntoConstraints = NO;

这将设置是否应将视图的自动调整大小掩码转换为基于约束的布局系统的约束。
我认为在您的示例中,必须使用use_alignment_insets,使用它更容易
- (UIEdgeInsets)alignmentRectInsets

不管怎样,如果您打算使用对齐方式,如果在我建议的更改之后进行调试,您将看到子视图的大小为0
- (CGRect)alignmentRectForFrame:(CGRect)frame

所以,我建议你设定一个开始的框架
view.frame = CGRectMake(50,50,200,200);

但这并不容易,你需要很好地计算位置,或者使用更多的约束来允许视图位于超级视图的中心。
另一件事是约束,这里您使用的代码将被弃用:
- (void)addConstraints:(NSArray *)constraints

相反,你需要和
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views];

NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views];

NSArray *constraints = [[NSArray arrayWithArray:horizontalConstraints] arrayByAddingObjectsFromArray:verticalConstraints];

[NSLayoutConstraint activateConstraints:constraints];

谢谢。

关于ios - 为什么-frameForAlignmentRect:和-alignmentRectForFrame:不调用IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27061654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com