【问题标题】:Wrong UIScrollView subviews placement错误的 UIScrollView 子视图放置
【发布时间】:2015-10-08 03:57:12
【问题描述】:

我正在以编程方式创建我的应用程序的 UI,我已经被这个问题困扰了将近两天了。

我的 MasterViewController 创建了我的滚动视图的一个实例:

CGRect bottomViewFrame = CGRectMake(0,self.view.bounds.size.height-self.view.bounds.size.height/4.6,self.view.frame.size.width,100);
BottomView *bottomView = [[BottomView alloc] initWithFrame:bottomViewFrame];

在我的 ScrollView 的 initWithFrame: 方法中,我有:

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    self.backgroundColor = [UIColor blackColor];
    self.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
    self.showsVerticalScrollIndicator = NO;
    self.pagingEnabled = YES;

    UIView* test = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    test.backgroundColor = [UIColor redColor];

    [self addSubview:test];

    return self;
}

有一些行为不正确... 首先,如您所见,我将 ScrollView 的 contentSize 高度设置为它自己的高度,但在模拟器上执行后我仍然能够滚动。

其次,我将 UIView“测试”设置为原点为 (0;0),但它显​​示的坐标在我看来像 (0;self.frame.height/2)。

这是模拟器上的最终结果

有人知道吗?提前感谢,非常感谢。

【问题讨论】:

  • 我尝试重构整个代码并删除随机内容以了解导致错误的原因。我最终为主窗口 rootViewController 提供了 MasterViewController,而不是之前的 UINavigationController 实例。似乎滚动视图现在遵循预期的行为但是......什么? UINavigationController 如何影响 UIScrollView?如何同时使用两者?

标签: ios objective-c iphone uiview uiscrollview


【解决方案1】:

解决了!经过两天的努力,我终于得到了答案。 UINavigationController 让 MainViewController 自动设置 UIScrollView 的子视图插入,我只需要插入到我的 MasterViewController 的 init 方法中

self.automaticallyAdjustsScrollViewInsets = NO;

解决问题,让我的观点随心所欲。

【讨论】:

  • 就是这样!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 1970-01-01
  • 2013-07-22
相关资源
最近更新 更多