2015-11-01 90 views
1

我有一個UIView矩形大小(414,736)。我製作了一個寬度爲ScrollView的寬度爲50,高度爲50的高度比UIView的大。當我將UIView添加到ScrollView時,UIView保持水平狀態是確定,但在垂直方向上它位於ScrollView的底部。所以我的滾動部分是最重要的部分。但我想UIView留在頂部,底部成爲滾動區域。 如何將UIView的特定位置放在ScrollView上? 以下代碼顯示了ScrollView的實現方式。如何將視圖添加到特定位置的滾動視圖?

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    self.scrollView=[[UIScrollView alloc] initWithFrame:screenRect]; 
    self.scrollView.delegate = self; 
    self.scrollView.clipsToBounds = YES; 
    [self.scrollView addSubview:self.drawChart]; 
    self.scrollView.contentSize=CGSizeMake(screenRect.size.width+100,screenRect.size.height+50); 
    self.scrollView.decelerationRate = UIScrollViewDecelerationRateFast; 
    self.scrollView.backgroundColor = [UIColor colorWithRed:0.564 green:0.439 blue:0.561 alpha:1]; 
    self.view=self.scrollView; 

編輯:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    //Database 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = [dirPaths objectAtIndex:0]; 
    // Build the path to the database file 
    databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent: @"RECORDS.db"]]; 
    //scroll view 
    // 
    self.scrollView=[[UIScrollView alloc] initWithFrame:screenRect]; 
    self.scrollView.delegate = self; 
    self.scrollView.clipsToBounds = YES; 

    [self.scrollView addSubview:self.drawChart]; 
    CGRect frame = self.drawChart.frame; 
    frame.origin = CGPointZero; 
    self.drawChart.frame = frame; 
    self.scrollView.contentSize=CGSizeMake(screenRect.size.width+100,screenRect.size.height+50); 
    self.scrollView.decelerationRate = UIScrollViewDecelerationRateFast; 
    self.scrollView.backgroundColor = [UIColor colorWithRed:0.564 green:0.439 blue:0.561 alpha:1]; 
     self.view=self.scrollView; 
} 

感謝

回答

1

添加chartview到滾動視圖後

[self.scrollView addSubview:self.drawChart]; 

做到以下幾點:

CGRect frame = self.drawChart.frame; 
frame.origin = CGPointZero; 
self.drawChart.frame = frame; 
+0

不,它不起作用。 self.drawChart.frame具有(0,0)原點,儘管我沒有實現它。 – batuman

+0

好吧...圖表視圖位於多遠? –

+0

50,這是我爲ScrollView添加的大小比UIView大。 – batuman