Ios UIScrollView自动旋转问题-子视图坚持原始位置,同时移动到新位置
在我的iOS应用程序中,它的一部分是视频播放器(使用YouTube API)。我决定在代码中使用滚动视图来布局这个视图,以确保在iPad版本的应用程序中,视图在所有旋转中都正确布局。在继续之前,下面是我的子视图布局代码。每当我的视图出现在屏幕上时,我就调用它Ios UIScrollView自动旋转问题-子视图坚持原始位置,同时移动到新位置,ios,objective-c,layout,uiscrollview,autorotate,Ios,Objective C,Layout,Uiscrollview,Autorotate,在我的iOS应用程序中,它的一部分是视频播放器(使用YouTube API)。我决定在代码中使用滚动视图来布局这个视图,以确保在iPad版本的应用程序中,视图在所有旋转中都正确布局。在继续之前,下面是我的子视图布局代码。每当我的视图出现在屏幕上时,我就调用它 - (void)setupView { // Set up Open in YouTube App button self.openInYouTubeAppButton = [[UIBarButtonItem alloc] initWit
- (void)setupView {
// Set up Open in YouTube App button
self.openInYouTubeAppButton = [[UIBarButtonItem alloc] initWithTitle:@"Open in YouTube App" style:UIBarButtonItemStylePlain target:self action:@selector(openInYouTubeApp)];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"youtube://"]]) {
self.navigationItem.rightBarButtonItems = @[self.shareButton, self.openInYouTubeAppButton];
} else {
self.navigationItem.rightBarButtonItems = @[self.shareButton];
}
self.scrollView.frame = self.view.frame;
// Set up video embed view
self.videoEmbedView = ({
// Frame
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 432)];
webView.center = CGPointMake(self.scrollView.center.x, (webView.bounds.size.height / 2));
// View Properties
webView.scrollView.contentInset = UIEdgeInsetsMake((-72 + 64), -8, 0, 0); // makes sure the video is fully seen in the view at first glance
webView.scrollView.scrollEnabled = NO; // doesn't allow the user to mistakenly scroll the video
webView;
});
self.videoTitleLabel = ({
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, (self.videoEmbedView.bounds.size.height + 14), 0, 0)]; // initial frame
label.text = self.videoTitle; // text is video title
label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; // dynamic type style
label.preferredMaxLayoutWidth = self.view.bounds.size.width; // makes sure label text does not extend outside the view.
[label sizeToFit];
label.center = CGPointMake(self.view.center.x, (self.videoEmbedView.bounds.size.height + 14 + (label.bounds.size.height / 2))); // set center
label;
});
self.videoDateLabel = ({
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, (self.videoEmbedView.bounds.size.height + 14 + self.videoTitleLabel.bounds.size.height + 8), 0, 0)]; // initial frame
label.text = [NSDateFormatter localizedStringFromDate:self.videoDate dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];
label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]; // dynamic type style
label.preferredMaxLayoutWidth = self.view.bounds.size.width; // makes sure label text does not extend outside the view.
[label sizeToFit];
label.center = CGPointMake(self.view.center.x, (self.videoEmbedView.bounds.size.height + 14 + self.videoTitleLabel.bounds.size.height + 8 + (label.bounds.size.height / 2))); // set center
label;
});
self.videoDescriptionTextView = ({
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, self.videoEmbedView.bounds.size.height + 14 + self.videoTitleLabel.bounds.size.height + 8 + self.videoDateLabel.bounds.size.height + 25, 768, 200) textContainer:nil]; // initial frame
textView.text = self.videoDescription;
textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
textView.center = CGPointMake(self.view.center.x, (self.videoEmbedView.bounds.size.height + 14 + self.videoTitleLabel.bounds.size.height + 8 + self.videoDateLabel.bounds.size.height + 25 + (textView.bounds.size.height / 2))); // set center
textView;
});
// Update scroll view content size
[self.scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, (self.videoEmbedView.bounds.size.height + 14 + self.videoTitleLabel.bounds.size.height + 8 + self.videoDateLabel.bounds.size.height + 25 + self.videoDescriptionTextView.bounds.size.height))];
// Add views to scroll view
[self.scrollView addSubview:self.videoEmbedView];
[self.scrollView addSubview:self.videoTitleLabel];
[self.scrollView addSubview:self.videoDateLabel];
[self.scrollView addSubview:self.videoDescriptionTextView];
// Set description text view frame to content size
CGRect frame = self.videoDescriptionTextView.frame;
frame.size.height = self.videoDescriptionTextView.contentSize.height;
self.videoDescriptionTextView.frame = frame;
// Update scroll view content size
[self.scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, (self.videoEmbedView.bounds.size.height + 14 + self.videoTitleLabel.bounds.size.height + 8 + self.videoDateLabel.bounds.size.height + 25 + self.videoDescriptionTextView.bounds.size.height))];
// Load video into embed view
NSString *embed = [NSString stringWithFormat:@"<iframe width='%f' height='%f' src='http://www.youtube.com/embed/%@' frameborder='0' allowfullscreen></iframe>", 768.0, 432.0, self.videoID];
[self.videoEmbedView loadHTMLString:embed baseURL:self.videoURL];
}
但这并不像我所希望的那样有效,我希望所有的东西都是中心对齐的,甚至是在景观中,但我得到了以下结果:
如您所见,视图将保持其以前的位置,并显示在新位置;视频嵌入视图甚至根本不移动
有人能帮我吗?我被卡住了,我到处寻找一些关于如何在autorotation中处理UIScrollView子视图的指导,所以现在我认为是时候对我的情况给出一些具体的答案了。提前感谢您的帮助。这个问题没有简单的“修复这行代码”修复方法。根据我的经验,在滚动视图中进行这种旋转调整的最佳方法是使用auto layout在UIScrollView中对视图进行排序,并使用auto layout根据滚动视图的容器调整视图的大小 从您的代码中,您似乎得到了以下视图结构:
UIScrollView (a)
UIWebView (b)
UILabel (c)
UILabel (d)
UILabel (e)
etc.
我将创建一个稍微不同的结构:
UIView (Z)
UIScrollView (a)
UIWebView (b)
UILabel (c)
UILabel (d)
UILabel (e)
etc.
使用自动布局语法,我将执行以下约束:H:|[Z]|和V:|[Z]|和H:|[a]|和V:|[a]|
有了这些,容器UIView和UIScrollView将始终填充它们分配的空间。
然后,对于内容的间距,我将执行以下操作:
五:|[b][c][d][e]|
H:|[b]
H:|[c]
H:|[d]
H:|[e]
您可能希望将“a”的大小设置为固定高度,以适应视频,但一旦这样做,其余视图将按高度流动。水平轴约束(例如H:|[a])告诉自动布局,所有视图都固定在UIScrollView(其父视图)的前缘
下一部分实际上不能用auto layout visual format语言表达,但您要做的是创建约束,以便UIScrollView中每个视图的宽度(b、c、d和e)与容器UIView(Z)的宽度相同
当自动布局发挥其布局魔力时,它将使用内容的固有高度垂直布局视图,并使用容器的宽度水平调整视图的大小。非常感谢!!!我从没想过使用自动布局!我最终只是使用故事板来实现这些约束,并去掉了大部分创建代码。再次非常感谢!!
UIView (Z)
UIScrollView (a)
UIWebView (b)
UILabel (c)
UILabel (d)
UILabel (e)
etc.