【问题标题】:UIScrollView in container view has incorrect frame size容器视图中的 UIScrollView 的帧大小不正确
【发布时间】:2013-05-25 18:40:39
【问题描述】:

我有一个带有 Frame = {X=20,Y=88,Width=728,Height=660} 的 UIContainerView 和一个嵌入其中的 UIScrollView。

UIScrollView 报告 Frame = {X=0,Y=0,Width=768,Height=960} 导致分页 UIScrollView 时宽度偏移 40(金色部分)。它导致以下输出向左分页一次:

但是,ContentSizeForViewInPopover 会报告预期的帧大小 = {Width=728, Height=660}。如何更正 UIScrollView 框架以具有正确的大小?

这是嵌入式 UIViewController 的 ViewDidLoad() 的代码:

var pages = PageControl.Pages = 2;
Console.WriteLine("ScrollView Frame: {0}", ScrollView.Frame);

for(int i = 0; i < pages; i++)
{
    RectangleF frame
    frame.X = ScrollView.Frame.Width * i; // expected {728 * i} but is {768 * i}
    frame.Y = 0;
    frame.Size = ScrollView.Frame.Size;

    Console.WriteLine("Subview frame: {0}", frame);

    ScrollView.AddSubview(
        new UIView(frame) { 
            Bounds = frame,
            BackgroundColor = GetRandomColor() 
        });
    }

    ScrollView.ContentSize = new SizeF(ScrollView.Frame.Width * pages, ScrollView.Frame.Height);
    Console.WriteLine("ScrollView ContentSize = {0}", ScrollView.ContentSize);

【问题讨论】:

  • UIScrollView 在代码中的什么位置创建?您必须将其 Frame(不是 ContentSize!)设置为超级视图的边界并为其提供适当的自动调整大小设置。
  • UIScrollView 设置在 UIView 上,该 UIView 位于 StoryBoard 上的 UIContainerView 内。 UIScrollView 没有显式创建。

标签: ios xamarin.ios uikit xamarin


【解决方案1】:

调用 [self.ScrollView layoutIfNeeded];在这行之前

frame.X = ScrollView.Frame.Width * i; // expected {728 * i} but is {768 * i}

它对我有用,祝你好运

【讨论】:

    【解决方案2】:

    仔细检查 IB 在父 UIViewController 下的 Attributes Inspector 的 Layout 部分下,是否未选中“Adjust Scroll View Insets”。我遇到了一个类似的问题,让我很困惑,这就是我需要做的所有事情来获得正确的布局。

    【讨论】:

      【解决方案3】:

      在 IOS 7.0 中 使用 self.automaticallyAdjustsScrollViewInsets=NO;在 ViewDidLoad 中

      【讨论】:

        【解决方案4】:

        尝试在代码中创建您的 UIScrollView,显然设计器会覆盖您在代码中应用的任何调整。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-01-06
          • 2018-05-09
          • 2021-05-09
          • 2012-07-05
          • 2013-06-07
          • 2013-04-16
          • 1970-01-01
          相关资源
          最近更新 更多