【问题标题】:UIScrollView with 3.5inch ScreenUIScrollView 3.5英寸屏幕
【发布时间】:2014-05-29 14:30:15
【问题描述】:

我为 iPhone 创建了一个应用程序,它非常适合 iPhone 5/5S/5C 屏幕。但是,我尝试在 iPhone 4/4S(较小的屏幕)上使用 ScrollView。

我的视图嵌入在导航栏控制器中,也位于标签栏控制器中。我的滚动视图位于两者之间的空间中,当它位于 iPhone 5/5S/5C 中时,我不希望它滚动,因为它已经是完美的尺寸了。这部分工作正常。

但是,当我在模拟器上以较小的屏幕尺寸测试它时,我希望它能够滚动,但它根本不滚动。

我的滚动视图有一个 IBOutlet,我在 .m 文件中使用以下代码行来设置滚动视图。

if (self.view.bounds.size.height == 568)
{
    [_Scroller setContentSize:CGSizeMake(320, 1500)];
    _Scroller.frame = CGRectMake(0, 64, 320, self.view.bounds.size.height);
}
else
{
    [_Scroller setContentSize:CGSizeMake(320, 80)];
    _Scroller.frame = CGRectMake(0, 64, 320, self.view.bounds.size.height);
}

_Scroller.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
[_Scroller setScrollEnabled:YES];

我的视图控制器中的 Adjust Scroll View Insets 也未勾选,因为它一直在滚动视图的顶部添加一个恼人的间隙。

谁能帮我解决我哪里出错了?它快把我逼疯了!任何帮助都会很棒。我对 iOS 开发也很陌生。

谢谢各位!

编辑:

我已经把 if 语句改成了这个,但还是不行:

if (self.view.bounds.size.height == 568)
{
    [_Scroller setContentSize:CGSizeMake(320, 1500)];
    _Scroller.frame = CGRectMake(-4, 64, 329, self.view.bounds.size.height);
}
else
{
    [_Scroller setContentSize:CGSizeMake(320, 2000)];
    _Scroller.frame = CGRectMake(-4, 64, 329, self.view.bounds.size.height);
}

_Scroller.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
[_Scroller setScrollEnabled:YES];

【问题讨论】:

  • 我认为你的 if 条件是错误的,检查你的 else 部分,你的 contentsize 太小,所以让它变大,这样你就可以滚动了。您的框架还可以,但 contentsize 太小。检查一下。
  • 我已经更改了代码,但仍然无法正常工作。我不明白我做错了什么。 @Prince 我看过那个链接,但有点困惑。

标签: ios iphone objective-c uiscrollview xcode5


【解决方案1】:

这样使用:

if ([[UIScreen mainScreen] bounds].size.height >= 568) //iphone 5/5c/5s/6/6 plus
{
  //making ContentSize and frame's height same as not need of scrolling
  //make changes in height if necessary
  [_Scroller setContentSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height-64)]; 
  //make changes in height if necessary
  _Scroller.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64); 
}
else //iphone 4/4s
{
   //making ContentSize greater than frame's height as we need scrolling
   //make changes in height if necessary
   [_Scroller setContentSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height+64)]; 
   if([[UIDevice currentDevice].systemVersion hasPrefix:@"7"]) //iOS 7.0 >
   {
     //made changes in y as status bar height is counted
     _Scroller.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64);
   }
   else //iOS 6.1 <
   {
      //made changes in y as status bar height not counted
      _Scroller.frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height-64);
   }
}

【讨论】:

  • 非常感谢@Prince,您为我节省了很多时间和痛苦!真的很感激!
猜你喜欢
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-10
  • 1970-01-01
相关资源
最近更新 更多