UIScrollView不尊重动态子视图位置

时间:2013-01-10 04:51:55

标签: ios interface-builder xcode4.5

编辑:2013年1月12日

Duplicate (answered) questiondemo project的来源已更新以反映。
我很乐意将这个问题标记为封闭,并提供一个正确的'dup'链接,但我也没有名声。我是多么可悲的小男人......


免责声明:这是一个可能过于复杂的观点,我正在研究可能被误导的意图。我非常愿意接受这样的可能性,即我缺少一些关键的信息/技术诀窍来完成这项工作,尝试做一些iOS根本不会做的事情,或者我做错了。如果出现这些情况,我会高兴地 接受一个让我这么说服的答案,或者只是在我的头上扔一份文件/重书,我可能会转到RTFM的页码。

一般问题

对于较大的应用程序中的一个视图,我试图向用户呈现具有四个主要元素的垂直滚动屏幕。从上到下列出,它们如下;关于Thing(UITextView)的一些一般信息,相关Stuff的列表(UITableView,每个代表实际app中的潜在segue),一些更多信息(在示例中,UILabel)和可操作按钮(a的UIButton)。 TableView中相关Stuff的单元格数量取决于呈现的内容。所以我已经找到了一种方法来调整tableView的大小并相应地移动UILabel和UIButton(详见下文,在viewDidAppear:中完成)。一切正常,花花公子和滚动让我开心,但是当我按下按钮时,它会跳回到它在Interface Builder视图中占据的位置,完全无视我对其框架的程序化更改。

问题

为什么?
可以修复(如果是,如何)? 是否有不同的和/或更好的方式?

我想发布一些图片,显示究竟发生了什么,但我没有声誉,所以请拥有一个演示项目的完整资源(见上文)。随意查看代码,看看第一手的情况。

所有细节

(随意跳过这些)
我在Interface Builder(以及代码中)中放置的屏幕组织如下:

View Controller -- Doing what it does.
* UIView -- Used to set a static background.
  * UIScrollView -- Sized to the parent view, used for scrolling
    * UIView -- Width equal to ScrollView, height is arbitrarily large (1200 in my project)
                We shall call him "subScrollView"
      * Content
      * More Content

我必须在我的ViewController的viewDidAppear:方法中执行内容视图的大小/位置更改,因为更快地进行更改将无法实际显示视图。
我需要(或只是?)subScrollView的原因是,如果我的所有内容视图都是ScrollView的子视图,则每当滚动发生时,上述问题就会发生 - 例如。视图出现,表格调整大小,按钮移动,然后用户滚动场景,表格快速回到Interface Builder中给出的大小,按钮移回原位。

以下是我用来调整内容视图的代码:

whyEvenViewController.h

@interface whyEvenViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *wasteMoreSpaceLabel;
@property (weak, nonatomic) IBOutlet UIButton *aButton;

@property (nonatomic) NSInteger tableViewRowCount;

@end

whyEvenViewController.m

- (void)viewDidAppear:(BOOL)animated
{
    // Here's where we resize and move everything. If we attempt to make these
    // changes before this (e.g. in viewDidLoad, viewWillAppear:, or even
    // viewDidLayoutSubviews) they get changed back before this view transitions
    // onto the screen, be it from a Navigation Bar push, or opening the app.

    [super viewDidAppear:animated];

    // Get used to this guy. He's going to be helping us resize frames.
    CGRect newFrame;

    // Set the tableView's frame height to the height of its contentSize which
    // so the entire table (no mater how large it is) is visible.
    newFrame = self.tableView.frame;
    newFrame.size.height = self.tableView.contentSize.height;
    self.tableView.frame = newFrame;

    // Move the wasteMoreSpaceLabel to just below the tableView
    newFrame = self.wasteMoreSpaceLabel.frame;
    newFrame.origin.y = self.tableView.frame.origin.y + self.tableView.frame.size.height - 20;
    self.wasteMoreSpaceLabel.frame = newFrame;

    // Move aButton to just below the wasteMoreSpaceLabel
    newFrame = self.aButton.frame;
    newFrame.origin.y = self.wasteMoreSpaceLabel.frame.origin.y + self.wasteMoreSpaceLabel.frame.size.height + 8;
    self.aButton.frame = newFrame;

    // Set the size of the scroll view such that it ends 20 points below aButton.
    CGSize newSize = self.scrollView.contentSize;
    newSize.height = self.aButton.frame.origin.y + self.aButton.frame.size.height + 20;
    self.scrollView.contentSize = newSize;
}

感谢您攀登这一段文字,感谢您的时间。

0 个答案:

没有答案