ios - 如何将 viewWillLayoutSubviews 转换为 viewWillTransitionToSize
<p><p>我正在使用 viewWillLayoutSubviews 来检测方向变化,但在 iOS 8 和 Xcode 6 中已弃用,我需要使用新的 viewWillTransitionToSize。我的问题是我无法使用它。</p>
<p>我的 viewWillLayoutSubviews 是:</p>
<pre><code>- (void)viewWillLayoutSubviews{
NSInteger maxQuantityForWidth = MAX(3, );
CGFloat buttonWidth = (CGRectGetWidth(self.tabBar.bounds) - (kBarButtonSeparation * (maxQuantityForWidth - 1))) / maxQuantityForWidth;
for (NSInteger i = 0; i < ; i++) {
setFrame:CGRectMake(i * ((buttonWidth + kBarButtonSeparation)), 2.0, buttonWidth, CGRectGetHeight(self.tabBar.bounds) - 2.0)];
}
if () {
self.functionalityContainer.frame = CGRectMake(0.0, CGRectGetHeight(self.tabBar.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.tabBar.bounds));
} else {
self.functionalityContainer.frame = self.view.bounds;
}
if (IS_IPAD){
;
CGRect frame = self.slidingViewController.topViewController.view.frame;
if (LANDSCAPE) {
frame.size.width = 700;
} else {
frame.size.width = 768;
}
;
}
}
</code></pre>
<p>它是使用 ECSLidingViewController 的导航部分的一部分。</p>
<p>我尝试了以下方法:</p>
<pre><code>- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
;
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
if (IS_IPAD){
CGRect frame = self.slidingViewController.topViewController.view.frame;
if (LANDSCAPE) {
NSInteger maxQuantityForWidth = MAX(3, );
CGFloat buttonWidth = (758 - (kBarButtonSeparation * (maxQuantityForWidth - 1))) / maxQuantityForWidth;
for (NSInteger i = 0; i < ; i++) {
setFrame:CGRectMake(i * ((buttonWidth + kBarButtonSeparation)), 2.0, buttonWidth, CGRectGetHeight(self.tabBar.bounds) - 2.0)];
}
frame.size.width = 768;
} else {
NSInteger maxQuantityForWidth = MAX(3, );
CGFloat buttonWidth = (758 - (kBarButtonSeparation * (maxQuantityForWidth - 1))) / maxQuantityForWidth;
for (NSInteger i = 0; i < ; i++) {
setFrame:CGRectMake(i * ((buttonWidth + kBarButtonSeparation)), 2.0, buttonWidth, CGRectGetHeight(self.tabBar.bounds) - 2.0)];
}
frame.size.width = 768;
}
;
}
if(){
;
}else{
;
}
if (IS_IPAD && LANDSCAPE) {
self.slidingViewController.resetStrategy = ECNone;
animated:YES];
if (!self.slidingViewController.underLeftShowing) {
;
}
} else {
self.slidingViewController.resetStrategy = ECTapping | ECPanning;
animated:YES];
;
}
;
;
;
}];
NSString *orientation = [initWithFormat:@"Larghezza: %f Altezza: %f", self.view.frame.size.width, self.view.frame.size.height];
;
;
}
</code></pre>
<p>但它不起作用,当它是横向或纵向时,它似乎不“理解”。关于如何修复它的任何想法?</p>
<p>*** 编辑:</p>
<p>这里是我使用的定义</p>
<pre><code>#define LANDSCAPE UIInterfaceOrientationIsLandscape(self.interfaceOrientation)
#define LANDSCAPE_RIGHT .orientation == UIDeviceOrientationLandscapeLeft
#define LANDSCAPE_LEFT .orientation == UIDeviceOrientationLandscapeRight
#define PORTRAIT UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
#define PORTRAIT_REVERSE .orientation == UIDeviceOrientationPortraitUpsideDown
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这个问题似乎有点“老”,无论如何,至少随着 iOS9 的引入(尤其是多任务处理),不建议根据设备方向对当前屏幕格式下结论 - 相反,应该评估当前屏幕尺寸。以下指示屏幕格式是纵向还是横向:</p>
<pre><code>- (void) viewWillTransitionToSize : (CGSize) screenSize withTransitionCoordinator : (id<UIViewControllerTransitionCoordinator> _Nonnull) coordinator
{
BOOL isLandscapeOrientation = (screenSize.width > screenSize.height);
...
...
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何将 viewWillLayoutSubviews 转换为 viewWillTransitionToSize,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26697368/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26697368/
</a>
</p>
页:
[1]