gpt4 book ai didi

ios - 继承 UIImageView 并添加 UIControl

转载 作者:行者123 更新时间:2023-11-28 22:41:44 26 4
gpt4 key购买 nike

我正在尝试对 UIImageView 进行子类化以向其添加 UIControl,以便我知道它何时被选中:

-(id)initWithSize:(CGSize)size OffImage:(UIImage *)offImage onImage:(UIImage *)onImage
{
if (self) {
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
self.userInteractionEnabled = TRUE;

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[self(80)]"
options:0
metrics:@{@"WIDTH" : [NSNumber numberWithFloat:size.width]}
views:NSDictionaryOfVariableBindings(self)]];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[self(90)]"
options:0
metrics:@{@"HEIGHT" : [NSNumber numberWithFloat:size.height]}
views:NSDictionaryOfVariableBindings(self)]];

self.offImage = offImage;
self.onImage = onImage;
self.selected = FALSE;

UIControl *control = [[UIControl alloc] init];
[control setTranslatesAutoresizingMaskIntoConstraints:NO];
self.control = control;

[self addSubview:control];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[control]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(control)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[control]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(control)]];

}

return self;
}

当我尝试添加约束以使 UIControl 与子类 UIImageView 的大小相同时,我收到一条错误消息,指出 self.control 没有 super View :

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
Unable to interpret '|' character, because the related view doesn't have a superview
H:|[control]|
^'

如果我通过创建一个 UIImageView 来做同样类型的事情,然后创建一个 UIControl 和 [imageView addsubview:control],它工作得很好。为什么当我尝试将相同的代码添加到 init 方法时它会崩溃?

最佳答案

您是如何创建 imageView 子类对象的?在这段代码中没有 [Super init..]

关于ios - 继承 UIImageView 并添加 UIControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14345869/

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