简体   繁体   English

设备方向改变时以编程方式重绘视图的简单方法

[英]Simple way to redraw view programatically when device orientation changes

I'm new to iOS development and for my assignment, I'm tasked changing updating the ViewController programmatically when the device's orientation changes. 我是iOS开发的新手,我的任务是在设备方向改变时以编程方式更改ViewController的更新。 I've found a snippet of an answer here, but it doesn't get the job done. 我在这里找到了一个答案的片段,但没有完成任务。

I tried adding this to the viewWillLayoutSubviews of my View Controller, but all I get is an unused variable warning. 我尝试将其添加到我的View Controller的viewWillLayoutSubviews中,但是得到的只是一个未使用的变量警告。

CGRect rotatedFrame = [self.view convertRect:self.view.frame fromView:self.view.superview];

viewWillLayoutSubviews and rotation viewWillLayoutSubviews和旋转

As a "hint", I've been told it's simple to implement in viewWillLayoutSubviews . 作为“提示”,有人告诉我在viewWillLayoutSubviews实现很简单。 Going through and changing all the CGRects in my VC doesn't sound like a couple of lines of code. 遍历和更改我的VC中的所有CGRect听起来并不像几行代码。 There's got to be a simpler, more efficient way to do this, but I've only found snippets of solutions digging around on this site. 必须有一种更简单,更有效的方法来执行此操作,但是我只在该站点上发现了解决方案的摘要。 Thanks for reading. 谢谢阅读。

The line of code you are using is assigning a CGRect to the rotatedFrame variable, it's not updating anything on your view controller. 您正在使用的代码行是将CGRect分配给rotatedFrame变量,它不会更新视图控制器上的任何内容。

There's many ways to approach this but it depends on what is contained in your view and how it's been configured. 有很多方法可以解决此问题,但这取决于视图中包含的内容以及配置方式。 Things like Auto Layout for example could let you configure almost everything in Interface Builder and let you avoid doing most things in code. 例如,诸如Auto Layout之类的事情可以让您在Interface Builder中配置几乎所有内容,并避免执行代码中的大多数事情。

You've been tasked to do this programatically and since we know that viewWillLayoutSubviews is called every time the device is rotated that's a good place to start. 您的任务是以编程方式执行此任务,因为我们知道每次旋转设备时都会调用viewWillLayoutSubviews因此这是一个不错的起点。 Here's a lazy way I've gone about rotating a video to fit a new orientation using a transformation: 这是我采用转换方式旋转视频以适应新方向的一种懒惰方式:

//Vertical
CGSize size = self.view.frame.size;
someView.transform = CGAffineTransformMakeRotation((M_PI * (0) / 180.0))
someView.frame = CGRectMake(0, 0, MIN(size.width, size.height), MAX(size.width, size.height));

//Horizontal
CGSize size = [UIScreen mainScreen].bounds.size;
int directionModifier = ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft) ? -1 : 1;
someView.bounds = CGRectMake(0, 0, MAX(size.width, size.height), MIN(size.width, size.height));
someView.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0) *directionModifier);
someView.transform = CGAffineTransformTranslate(someView.transform,0,0);

How many subviews are in your view? 您的视图中有多少个子视图? Are they grouped? 他们分组了吗? If you're using auto-resizing masks you might get away with only adjusting the frames of one or two views. 如果您使用的是自动调整大小的蒙版,则可能仅需调整一个或两个视图的帧即可。 If your root view has a number of subviews you can loop through views that need similar adjustments to avoid having to write excess code. 如果您的根视图包含许多子视图,则可以遍历需要进行类似调整的视图,从而不必编写过多的代码。 It really depends on how everything has been set up. 这实际上取决于如何设置所有内容。

I figured out how to determine the viewWidth and viewHeight and set those as CGFloats. 我想出了如何确定viewWidth和viewHeight并将它们设置为CGFloats的方法。 I then added an if-else statement which figures out if the display is in portrait or landscape and sets the problematic calculateButton accordingly. 然后,我添加了一条if-else语句,该语句确定显示是纵向还是横向,并相应地设置有问题的calculateButton。

Apologies for lengthy code, but I've found in searching this site I find "snippets" of answers, but being new to iOS it's difficult to figure out what goes where. 为冗长的代码而道歉,但是我在搜索该网站时发现答案的“摘要”,但是对于iOS来说,它是新手,很难弄清楚该怎么做。 Hopefully, this helps someone later. 希望这对以后的人有所帮助。 (and hopefully it's correct) (希望它是正确的)

- (void) viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    CGFloat viewWidth = self.view.bounds.size.width;
    CGFloat viewHeight = self.view.bounds.size.height;
    CGFloat padding = 20;
    CGFloat itemWidth = viewWidth - padding - padding;
    CGFloat itemHeight = 44;

// Bunch of setup code for layout items

    // HOMEWORK: created if-else statement to deal w/ portrait vs. landscape placement of calculateButton.

    if (viewWidth > viewHeight) {
        // portrait
        CGFloat bottomOfLabel = viewHeight;
        self.calculateButton.frame = CGRectMake(padding, bottomOfLabel - itemHeight, itemWidth, itemHeight);
    } else {
        // landscape
        CGFloat bottomOfLabel = CGRectGetMaxY(self.resultLabel.frame);
        self.calculateButton.frame = CGRectMake(padding, bottomOfLabel + padding, itemWidth, itemHeight);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方向更改时重绘自定义视图的最佳方法 - Best way to redraw a custom view when orientation changes 当设备方向改变时,导致子视图重绘的正确方法是什么? - what's the right way to cause a subview to redraw when the device orientation changes? 设备方向更改时调整PDF查看大小 - Resize PDF View when device orientation changes 设备方向更改时更改视图控制器 - changing view controller when the device orientation changes 更改设备方向时重绘自定义 uiview - redraw a custom uiview when changing a device orientation 设备方向改变时重绘 CoreGraphics 绘图 - Redraw CoreGraphics drawing when device orientation changed 设备方向更改时如何重新排列集合视图单元格 - How to rearrange collection view cell when device orientation changes 当设备方向迅速改变时,浮动操作按钮的视图不出现 - View of floating action button not appearing when device orientation changes in swift 当设备方向改变时以编程方式更新约束的正确方法吗? - Correct way to programmatically update constraints when the device orientation changes? 当方向改变时,以编程方式更改UISearchBar的框架 - Change frame of UISearchBar programatically when orientation changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM