2013-03-04 105 views
0

我在同一個問題上看過很多不同的主題,似乎無法理解這裏發生了什麼。我創建了一個非常簡單和簡單的項目。我想要發生的是一組元素位於滾動視圖內部,可以正確滾動。這些元素可能包括獨立滾動(或不滾動)的UITableView。下面是我如何設置我的基本項目:關於UIScrollView和AutoLayout的困惑

IB View

在模擬器中運行,這將導致不滾動視圖。這是預料之中的,因爲所有元素都有足夠的空間。後來我改變的UIScrollView的大小,像這樣:

IB View 2

在不會滾動較小視圖仿真結果運行此。我認爲我不是這樣做的正確方法。以下是我最近嘗試了代碼:

ViewController.h

@interface ViewController : UIViewController <UIScrollViewDelegate> 

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView; 
@property (strong, nonatomic) IBOutlet UIButton *buttonOne; 
@property (strong, nonatomic) IBOutlet UIButton *buttonTwo; 
@property (strong, nonatomic) IBOutlet UIButton *buttonThree; 
@property (strong, nonatomic) IBOutlet UIButton *buttonFour; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    [self.buttonOne setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.buttonTwo setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.buttonThree setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.buttonFour setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.scrollView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

任何幫助將不勝感激。

回答

3

問題是,當您更改界面構建器中滾動視圖的大小時,您還將內容的大小設置爲與框架大小相同。

您可以手動增加在代碼中滾動視圖的內容大小,這樣就可以像這樣把它的滾動:

// scrollView is an outlet to your scroll view 
// Add 200 points to the bottom of the scroll view 
[self.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 200, 0)]; 

雖然,你可能不爲你畫的需要,只要做到這一點子視圖的大小,並使其成爲其他視圖的子視圖,並讓它自動重新調整大小。

+0

謝謝!我把所有的UI元素(按鈕,表格)放在一個'UIView'中,它是'UIScrollView'的子視圖。沒有任何額外的代碼,一切都很好。 – Alex 2013-03-06 15:37:26