现在的位置: 首页 > 综合 > 正文

防止UIScrollView的自动偏移

2018年04月15日 ⁄ 综合 ⁄ 共 3381字 ⁄ 字号 评论关闭
文章目录

在该ViewDidLoad设置这个viewController:self.automaticallyAdjustsScrollViewInsets = NO

http://www.cocoachina.com/ask/questions/show/112647

很简单的问题UIScrollView addSubView居然出问题!!!

很简单的问题UIScrollView addSubView居然出问题!!!

0

0关注

顶起好的问题被+5荣誉值给作者并让问题出现在首页!贡献您的力量!社区积分跟问答荣誉值已全线对接!

我新建了一个带有xib的TestViewController,然后在它的viewDidLoad里面添加了如下代码:

UIScrollView *myScroll = [[UIScrollView
alloc] initWithFrame:CGRectMake(30,
180, 100, 74)]; 

  myScroll.backgroundColor = [UIColor
lightGrayColor];

    UIView *sbv = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 30, 30)];

    sbv.bounds = CGRectMake(0,
0, 30, 30);

    sbv.backgroundColor = [UIColor
brownColor];

    [myScroll addSubview:sbv];

    [self.view
addSubview
:myScroll];

然后在appDelegate里面让self.window.rootViewController =我的这个viewController。这时sbv能正常显示在myScroll上。

但是,当我在appDelegate里面这么做时就出问题了:

TestViewController *tvc = [[TestViewController
alloc] init];

    UINavigationController *nvc = [[UINavigationController
alloc] initWithRootViewController:tvc];

    [self.window
setRootViewController:nvc];

这时为毛sbV这么跑myScroll下面去了??sbv的y坐标貌似与myScroll的左上角的y坐标相差44!我X!请问我这哪里出问题了??

5 个回答

回复问题被顶起+10个荣誉值奥!

1 最佳答案
当iOS系统为7时,在你说得那种用导航控制器为根视图的情况下,加载的UIScrollView会发生改变,改变情况为bounds = {{0, -64}, {100, 74}}contentInset = {64, 0, 0, 0}contentOffset: {0, -64}所以向下偏移了,具体为什么会变还得研究下,在ViewDidLoad方法中看不出来,你需要写个print方法,用[self performSelector:@selector(print)
withObject:nil afterDelay:1.0f];延迟一秒再输出才能看出来..
  • 解决了。。。。在该ViewDidLoad设置这个viewController:self.automaticallyAdjustsScrollViewInsets = NO;就OK了
    无助的tom 2014-05-29 13:53
  • Good job!
    nb_tpl 2014-05-29 14:08
  • 回复

1
CGRectMake写到didappear里 不要 写在viewdidload里
  • 果然是高人啊,我把那些代码放到viewDidAppear里面就好了。请问这是什么原因啊??平时UiView添加子视图时都是在viewDidload里面添加的,也没什么问题,我就是发现UiScrollView在viewDidLoad里面添加子视图就出问题,以前咋没发现
    无助的tom 2014-05-28 09:58
  • 还是这句话,viewDidLoad里的frame不准http://www.cocoachina.com/ask/questions/show/109918#41252
    wfqr0001 2014-05-28 10:02
  • 我设置的frame并不是viewController自身的frame啊,我只是创建一个子视图myScroll,只是设置myScroll的frame,如果将myScrollView换成UiView,然后给这个UiView添加子视图就没有问题。正如楼下说的“用导航控制器为根视图的情况下,加载的UIScrollView会发生改变”
    无助的tom 2014-05-28 10:28
  • 回复

1

因为你添加了UINavigationController

系统默认的navigationbar的高度就是44,你如果把你的ViewController添加到了navigationController里的话,所有在ViewController里的View的Origin Y 都会加44.

 

0

TestViewController *tvc = [[TestViewController allocinit 

这儿检查一下,要用nib加载你的vc吧。。不是alloc]init 方法创建。。你试试。。

  • 没啥变化。。。。我这个非常非常简单,就这么多东西。
    无助的tom 2014-05-27 23:31
  • 没啥变化。。。。我这个非常非常简单,就这么多东西。你可以复制一下代码试试看,
    无助的tom 2014-05-27 23:32
  • 回复

0
那你就用上面那种方法添加呗,解决了问题就ok了 
  • 没啥变化意思是还是老样子,并没有解决。。
    无助的tom 2014-05-27 23:47
  • 回复

    你的回答

    CocoaChina会员:无助的tom 期待您帮忙来解答这个问题,一起来帮忙消灭零回复的问题吧!

    登陆|
    注册

抱歉!评论已关闭.