gpt4 book ai didi

ios - 添加图像后 UIButtonTypeCustom 不再起作用

转载 作者:行者123 更新时间:2023-11-29 04:18:24 26 4
gpt4 key购买 nike

我有一个里面有图像的按钮,当我按下按钮时我想要一个弹出窗口。当按钮为空,内部没有图像时,一切正常,我现在添加了图像,按钮不再工作。按钮对应于 IBAction,并通过 IBOutlet 连接到 XIB。我能做什么?

 - (void)viewDidLoad
{
//.....
self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];

if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){

self.myButton.frame = CGRectMake(160, 502, 73, 33);
[self.myButton setImage:[UIImage imageNamed:@"iphone_legenda.png"]
forState:UIControlStateNormal];
self.myButton.contentMode = UIViewContentModeScaleToFill;

}else{

self.myButton.frame = CGRectMake(330, 917, 182, 68);
[self.myButton setImage:[UIImage imageNamed:@"ipad_legenda.png"]
forState:UIControlStateNormal];
self.myButton.contentMode = UIViewContentModeScaleToFill;
}


[self.view addSubview:self.myButton];
/*The problem arose in this line, without addSubview the image you could not see
but the button worked, added addSuview the image you see but the button does not
work anymore*/

NSLog(@"The button is %@", self.myButton);

/*The button is <UIButton: 0x80f1e90; frame = (330 917; 182 68); opaque = NO;
layer = <CALayer: 0x80f1210>>*/

//.......

}

-(IBAction)pressedButton:(id)sender{
//This method initially worked
}

最佳答案

如果该按钮已在 XIB 中定义,您的代码将创建另一个不同的按钮,而不附加任何事件 (IBAction)。

您可以将代码更改为:

- (void)viewDidLoad
{
//.....

if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){

self.myButton.frame = CGRectMake(160, 502, 73, 33);
[self.myButton setImage:[UIImage imageNamed:@"iphone_legenda.png"]
forState:UIControlStateNormal];
self.myButton.contentMode = UIViewContentModeScaleToFill;

}else{

self.myButton.frame = CGRectMake(330, 917, 182, 68);
[self.myButton setImage:[UIImage imageNamed:@"ipad_legenda.png"]
forState:UIControlStateNormal];
self.myButton.contentMode = UIViewContentModeScaleToFill;
}

}

其他选项是有两个 XIB。一款适用于 iPad,另一款适用于 iPhone。

关于ios - 添加图像后 UIButtonTypeCustom 不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13344675/

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