如何将视图添加到特定位置的ScrollView?

时间:2015-11-01 11:03:01

标签: ios objective-c uiview uiscrollview

我有UIView,矩形大小为(414,736)。我的宽度为ScrollView 50,高度为50 UIView。当我向UIView添加ScrollView时,UIView水平停留确定,但在垂直方向上,它位于ScrollView的底部。所以我的滚动部分是顶部。但我希望UIView留在顶部而底部则成为滚动区域。 如何找到具有特定位置的UIViewScrollView? 以下代码显示了如何实现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 个答案:

答案 0 :(得分:1)

将chartview添加到scrollview

[self.scrollView addSubview:self.drawChart];

执行以下操作:

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