繁体   English   中英

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

[英]How to add a View to a ScrollView at a specific position?

我有一个UIView ,矩形大小为(414,736)。 我使ScrollView的宽度比UIView大50,高50。 当我将UIView添加到ScrollViewUIView保持水平是可以的 ,但在垂直方向上它位于ScrollView的底部。 因此,我的滚动部分是顶部。 但是我希望UIView停留在顶部,而底部成为滚动区域。 如何在ScrollView上找到具有特定位置的UIView 以下代码显示了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;
}

谢谢

将chartview添加到scrollview之后

[self.scrollView addSubview:self.drawChart];

请执行下列操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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