2014-12-27 42 views
1

我在編程上在UIScrollView的視圖中創建按鈕。我也以編程方式創建UITextField。我可以選擇並輸入數據到文本字段整天。但我無法得到按鈕被觸摸的任何標誌。我可以將它們添加到主視圖中並且它們可以工作。但是,如果將其添加到UIScrollView內部的視圖中,它們會在另一個維度中丟失。我已閱讀大量有關設置userInteractiondelayContentTouches以及通過gestureRecognizer攔截的信息。最後可能與我的問題有關。我通過[touch.view isDescendantOfView:self.myScrollViewName]返回NO。我無法通過[touch.view isKindOfClass:[UIButton class]]觸發它。這讓我覺得我有一個更重要的錯誤,我錯過了?在UIScrollView中無法觸摸UIButton

相關: https://stackoverflow.com/a/6825839/4050489

編輯以每個請求添加按鈕的代碼:

 self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     self.myButton.frame = myButtonFrame; 
     self.myButton.enabled = interface.isActive; 
     self.myButton.userInteractionEnabled = YES; 
     self.myButton.exclusiveTouch = YES; 

     self.myButton.backgroundColor = [UIColor greenColor]; 

     //self.myButton. = YES; 

     self.myButton.layer.borderWidth=1.0f; 
     self.myButton.layer.borderColor=[[UIColor blackColor] CGColor]; 
     self.myButton.layer.cornerRadius = 10; 

     [self.myButton setTag:interface.tag]; 
     [self.myButton setTitle:interface.Name forState:UIControlStateNormal]; 

     [self.myButton addTarget:self action:@selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside]; 

     [self.myButton setEnabled:YES]; //error check 


     [self.partSetupView addSubview:self.myButton]; 

partSetupView處於UIScrollView一個UIView

+1

請張貼一些代碼樣本,以你是如何將這些按鈕將滾動型 – jervine10 2014-12-27 14:22:42

+0

您可以明確地啓用'showsTouchWhenHighlighted',看看它甚至識別向下觸摸事件。 – jervine10 2014-12-27 15:06:16

+0

我不認爲它確實承認它。除非添加到「UIScrollView」之外的'UIView',否則您甚至不會獲得文本閃爍。另外它不會導致委託人觸發。 「UIScrollView」干擾或沒有正確連接,但我迄今爲止閱讀的解決方案似乎不能防止這種干擾。似乎是一個常見問題。但文本字段毫不費力地工作。 – Bill3 2014-12-27 15:17:20

回答

2
Try this sample Code: 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)]; 
    myScroll.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:myScroll]; 

    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)]; 
    myView.backgroundColor = [UIColor yellowColor]; 
    [myScroll addSubview:myView]; 

    UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)]; 
    myButton.backgroundColor = [UIColor blackColor]; 
    [myView addSubview:myButton]; 
    [myButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchDown]; 
} 
-(void)btnClick{ 
    NSLog(@"Button Click"); 
} 
@end 
+1

這有助於找到答案。你的按鈕工作。我沒有。他們之間沒有真正的區別。除位置。儘管看到按鈕,儘管他們滾動,我沒有我的'UIView'框架(partSetupView框架大小)設置爲我的'UIScrollView'相同。我已經看到了這一點,但我不明白他們的意思,直到我自己與之戰鬥。 – Bill3 2014-12-27 18:15:20

+0

給你一個提升,因爲你的評論把我放在正確的道路上! – PruitIgoe 2017-03-16 14:07:56