【问题标题】:IOS - Centering UIScrollView inside UIView container programmatically with Autolayout issueIOS - 以编程方式在 UIView 容器内居中 UIScrollView 并带有 Autolayout 问题
【发布时间】:2013-03-26 11:40:29
【问题描述】:

我正在尝试复制 Safari 页面滚动视图,它的框架比 iPhone 屏幕略窄。所以根据一些建议,我将滚动视图(450x280 点)放在“clipView”(450x320 点)内。

//viewController methods
-(void)viewDidAppear:(BOOL)animated{

CGRect pagingScrollViewFrame = self.clipview.frame;
pagingScrollViewFrame.size.width = 280;

NSLog(@"clipview h:%f e w:%f",self.clipview.frame.size.height,self.clipview.frame.size.width);
NSLog(@"scrollview h:%f e w:%f",pagingScrollViewFrame.size.height,pagingScrollViewFrame.size.width);
NSLog(@"center clipview :%@",NSStringFromCGPoint(self.clipview.center));

pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
NSLog(@"center scrollview:%@",NSStringFromCGPoint(pagingScrollView.center));
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width *3, pagingScrollViewFrame.size.height);

pagingScrollView.clipsToBounds = NO;
pagingScrollView.center = self.clipview.center;

    [clipview addSubview:pagingScrollView];
}

如您所见,我的 scrollView 中心向下移动了 20 点 ({140, 232})。 我的视图 Container 的中心位于 {160, 232}

我哪里错了?我应该使用什么类似内容偏移属性的东西?这个问题与自动布局有关吗?

【问题讨论】:

    标签: objective-c uiview ios6 uiscrollview autolayout


    【解决方案1】:

    问题出在这一行:

    pagingScrollView.center = self.clipview.center;
    

    应该是:

    pagingScrollView.center = CGPointMake(CGRectGetMidX(self.clipview.bounds), CGRectGetMidY(self.clipview.bounds));
    

    将父视图的中心分配给其子视图是常见的错误。它们位于不同的坐标系中。

    【讨论】:

    • 现在当我 NSlog 中心时,它们看起来具有相同的坐标 {160,232} 但是当我运行应用程序时,滚动视图仍然向下移动 20 像素。
    猜你喜欢
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多