【问题标题】:UIScrollView height changeUIScrollView 高度变化
【发布时间】:2010-07-21 21:43:36
【问题描述】:

我有一个包含地图列表的 UITableView。当一个项目被选中时,一个新的子视图被添加到主窗口并显示地图。在我的地图视图的视图控制器中,每次显示时都会调用以下方法:

- (void)showMap {
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:mapImage]];

float imageHeight = tempImageView.image.size.height;
float targetHeight = scrollView.frame.size.height - mapNavigationController.navigationBar.frame.size.height;

scrollView.contentSize = tempImageView.image.size;
scrollView.maximumZoomScale = 1.0;
scrollView.minimumZoomScale = targetHeight / imageHeight;
scrollView.clipsToBounds = YES;

self.imageView = tempImageView;
[tempImageView release];

[scrollView addSubview:imageView];
scrollView.zoomScale = scrollView.minimumZoomScale;}

每次都会向 UIScrollView 添加一个新的 UIImageView,当视图消失时,UIImageView 会被移除。计算 zoomScale 以使图像根据纵横比进行拟合。

现在我的问题是,第一次单击项目时一切正常,地图图像填充 UIScrollView 的整个高度,但下次单击项目时,UIScrollView 的高度更小。我还应该注意,主窗口包含一个标签栏。

现在回答我的问题:

  • UIScrollView 如何改变它的大小?
  • 还有其他动态方法来检索 targetHeight 吗?

提前致谢!

【问题讨论】:

    标签: iphone uiscrollview uiimageview


    【解决方案1】:

    我遇到了类似的问题。事实证明,问题在于我将UIScrollView 子类化为UIViewController 的主视图,而UIViewController 对滚动视图的正确大小有自己的想法。

    我通过将主视图子类设为UIView 而不是UIScrollView 来修复它,然后将UIScrollView* content_view 添加到视图中,并将我的子视图添加到content_view 而不是self

    不确定这是否对您有帮助,但是...

    【讨论】:

      【解决方案2】:

      好的,我自己设法找到了一个解决方案,也许不是最好的,但它是动态的并且有效。

      但我还是不明白为什么 UIScrollView 的高度会改变。如果有人知道原因,请发表评论。

      NSInteger scrollViewHeight = 0;
      
      - (void)showMap {
          if (scrollViewHeight == 0) {
              scrollViewHeight = scrollView.frame.size.height;
          } else {
              scrollView.frame.size.height = scrollViewHeight;
          }
      
          UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:mapImage]];
      
          float imageWidth = tempImageView.image.size.width;
          float imageHeight = tempImageView.image.size.height;
          float targetWidth = scrollView.frame.size.width;
          float targetHeight = scrollViewHeight - mapNavigationController.navigationBar.frame.size.height;
          float scale = targetHeight / imageHeight;
      
          if (scale * imageWidth < targetWidth) {
              scale = targetWidth / imageWidth;
          }
      
          scrollView.contentSize = tempImageView.image.size;
          scrollView.maximumZoomScale = 1.0;
          scrollView.minimumZoomScale = scale;
          scrollView.clipsToBounds = YES;
      
          self.imageView = tempImageView;
          [tempImageView release];
      
          [scrollView addSubview:imageView];
          scrollView.zoomScale = scale;}
      }
      

      【讨论】:

      • 可能你已经想通了,我只是把我的想法放在这里。正如我所认为的,这可能是因为您更改了 scrollView 的 zoomScale 高度被改变并且在所有情况下它变得越来越小:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 2015-01-21
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-10-03
      相关资源
      最近更新 更多