简体   繁体   中英

How to add a View to a ScrollView at a specific position?

I have a UIView with a rectangle size of (414,736). I made a ScrollView 50 in width and 50 in height bigger than UIView . When I add UIView to ScrollView , UIView stays horizontally is OK , but in vertical it is located at the bottom of the ScrollView . So that my scrolling part is the top part. But I want UIView stay on the top part and the bottom part becomes the scrolling area. How can I locate the UIView with a specific location onto the ScrollView ? The following code shows how ScrollView is implemented.

    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;

EDIT:

- (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;
}

Thanks

after adding the chartview to the scrollview

[self.scrollView addSubview:self.drawChart];

do the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM