2016-09-04 26 views
0

我有一個UIScrollView。在這個UIScrollView裏面是一個包含所有其他子視圖(包括UIButton)的UIView。它與UIScrollView具有相同的邊界。UIButton沒有響應當放在它的容器UIScrollView

我想:

  1. 的的UIScrollView的底部不滾動能(其中UIButton的放置)。
  2. UIButton是響應式的。

所以我設置的UIScrollView比的UIView如下圖像高一點的底部:

enter image description here

這將使的UIButton沒有反應(這是響應左側佈局)。任何建議?謝謝!

我的代碼一些部分:

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; 
[self addSubview:self.scrollView]; 
self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero]; 
[self.scrollView addSubview:self.scrollContent]; 

self.button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[self.scrollContent addSubview:self.button]; 
[self.button addTarget:self 
       action:@selector(buttonPressed) 
     forControlEvents:UIControlEventTouchUpInside]; 

更新:

貌似正確的方法是保持滾動視圖的底部,並設置其contentSize,但因爲我使用的自動佈局,我發現contentSize的高度始終爲零?

+0

看起來這是行不通的。另一個問題是我有一個按鈕最初放在滾動視圖之外。我滾動後,按鈕也沒有響應... – mlin956

回答

0

您需要將按鈕放置在滾動視圖視圖層次結構之外的視圖中。

當前命中測試失敗,因爲按鈕不在滾動視圖範圍內,而是在其層次結構中。

+0

但我希望按鈕可以與滾動動作交互... – mlin956

0

添加該按鈕滾動認爲,嘗試下面的代碼後:

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; 
    [self addSubview:self.scrollView]; 
    self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero]; 
    [self.scrollView addSubview:self.scrollContent]; 

     self.scrollView.delaysContentTouches = NO; //Add this line 

    self.button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [self.scrollContent addSubview:self.button]; 
    [self.button addTarget:self 
        action:@selector(buttonPressed) 
      forControlEvents:UIControlEventTouchUpInside];