[英]UIButton unresponsive when created programmatically and added to uiimageview
我的viewDidLoad方法中包含以下代碼。 這是帶有滾動視圖和頁面控制器的uiviewcontroller。
我有以下代碼從數組創建視圖,並在第3頁上放置一個按鈕:
for (int i = 0; i < imageNames.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
UIImageView *myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];
myImageView.image=[UIImage imageNamed:[imageNames objectAtIndex:i]];
[subview addSubview: myImageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[button addTarget:self
action:@selector(doSomething:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
if(i == 1){
[myImageView addSubview:button];
}
[self.scrollView addSubview:subview];
}
我有一個ibaction,它將以模態視圖關閉此視圖:-(IBAction)doSomething:(id)sender {[self dismissModalViewControllerAnimated:YES]; }
問題在於此按鈕無響應。 當觸摸更不用說調用doSomething方法時,它甚至不會突出顯示。
我究竟做錯了什么?
注知道這是問題,但可能。
從UIImageView文檔:
默認情況下,新的圖像視圖對象配置為忽略用戶事件。 如果要處理UIImageView的自定義子類中的事件,則必須在初始化對象后將userInteractionEnabled屬性的值顯式更改為YES。
為將來的搜索者的利益而發布...
如果沒有啟用UIImageView
的userInteractionEnabled
屬性,則需要查看的另一件事是UIButton的視圖是否在其超級視圖的框架之外。
當對象的視圖超出其視圖的范圍時,它將顯示給用戶,但將無法接收觸摸事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.