设备旋转后的iOSbutton点击区域

旋转后,我遇到了几个button的问题。 在纵向模式下,button堆叠在屏幕的底部。 在风景中时,它们被移动并resize以并排在屏幕的底部。 这一切都很好,button的行为正确。 但是,如果手机然后旋转回肖像,button的点击区域是错误的 – 只有button的左边1/3是可点击的。

这是做旋转的代码。 (我在viewWillAppear中调用它,所以当视图显示的时候,它总是被执行,以防视图已经在横向上)。我已经把button放在了它们自己的内部视图中。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { // determine our dimensions based on orientation CGRect mainRect = [[UIScreen mainScreen] bounds]; CGFloat maxWidthHeight = MAX(mainRect.size.width, mainRect.size.height); CGFloat minWidthHeight = MIN(mainRect.size.width, mainRect.size.height); CGFloat _windowWidth = (UIDeviceOrientationIsPortrait(toInterfaceOrientation)) ? minWidthHeight : maxWidthHeight; // get the current sizes of the things we are moving CGRect saveRect = self.viewButtons.frame; CGRect addLocRect = self.buttonAddLocation.frame; CGRect connectRect = self.buttonConnect.frame; // This will be set below in one of the if-else branches CGFloat buttonWidth = 0; // determine the offset from the left/right based on device and orientation int offset = 0; if ([self isIphone]) { offset = (UIDeviceOrientationIsPortrait(toInterfaceOrientation)) ? OFFSET_LEFT_PORTRAIT_IPHONE : OFFSET_LEFT_LANDSCAPE_IPHONE; } else { offset = (UIDeviceOrientationIsPortrait(toInterfaceOrientation)) ? OFFSET_LEFT_PORTRAIT_IPAD : OFFSET_LEFT_LANDSCAPE_IPAD; } // change the size & location of the button frame to just the button height to maximize the area for the location list // no matter what orientation, the button frame will fill the bottom of the screen saveRect.size.width = _windowWidth - 2*offset; saveRect.origin.x = offset; // for Landscape, move the buttons to side-by-side at the bottom of the window if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { // size & move the buttons to fit side-by-side buttonWidth = (saveRect.size.width)*.4; addLocRect.origin.x += offset; addLocRect.origin.y = saveRect.size.height - addLocRect.size.height ; connectRect.origin.x = saveRect.size.width - buttonWidth - offset; connectRect.origin.y = saveRect.size.height - connectRect.size.height; } else { // Portrait // move the buttons down to the bottom of the frame, stacked // size the buttons to be fully across the screen buttonWidth = saveRect.size.width; addLocRect.origin.y = 0 ; // at the top of the button view addLocRect.origin.x = 0; connectRect.origin.y = saveRect.size.height - connectRect.size.height; connectRect.origin.x = 0; } connectRect.size.width = buttonWidth; addLocRect.size.width = buttonWidth; self.buttonAddLocation.frame = addLocRect; self.buttonConnect.frame = connectRect; self.viewButtons.frame = saveRect; } 

我添加了一些日志logging来确定在旋转结束时帧和边界发生了什么:

  NSLog(@"Post rotation:"); NSLog(@"AddLocation bounds: w=%f, h=%f, x=%f, y=%f", self.buttonAddLocation.frame.size.width, self.buttonAddLocation.frame.size.height, self.buttonAddLocation.frame.origin.x, self.buttonAddLocation.frame.origin.y); NSLog(@"AddLocation frame: w=%f, h=%f, x=%f, y=%f", self.buttonAddLocation.bounds.size.width, self.buttonAddLocation.bounds.size.height, self.buttonAddLocation.bounds.origin.x, self.buttonAddLocation.bounds.origin.y); NSLog(@"viewButtons bounds: w=%f, h=%f, x=%f, y=%f", self.viewButtons.frame.size.width, self.viewButtons.frame.size.height, self.viewButtons.frame.origin.x, self.viewButtons.frame.origin.y); NSLog(@"viewButtons frame: w=%f, h=%f, x=%f, y=%f", self.viewButtons.bounds.size.width, self.viewButtons.bounds.size.height, self.viewButtons.bounds.origin.x, self.viewButtons.bounds.origin.y); 

导致:

 Post rotation: (NOTE: Portrait, initial appearance) AddLocation bounds: w=272.000000, h=55.000000, x=0.000000, y=0.000000 AddLocation frame: w=272.000000, h=55.000000, x=0.000000, y=0.000000 viewButtons bounds: w=272.000000, h=120.000000, x=24.000000, y=374.000000 viewButtons frame: w=272.000000, h=120.000000, x=0.000000, y=374.000000 Post rotation: (NOTE: rotate to Landscape) AddLocation bounds: w=217.600006, h=55.000000, x=12.000000, y=65.000000 AddLocation frame: w=217.600006, h=55.000000, x=0.000000, y=0.000000 viewButtons bounds: w=544.000000, h=120.000000, x=12.000000, y=374.000000 viewButtons frame: w=544.000000, h=120.000000, x=0.000000, y=374.000000 Post rotation: (NOTE: back to Portrait) AddLocation bounds: w=272.000000, h=55.000000, x=0.000000, y=0.000000 AddLocation frame: w=272.000000, h=55.000000, x=0.000000, y=0.000000 viewButtons bounds: w=272.000000, h=120.000000, x=24.000000, y=138.000000 viewButtons frame: w=272.000000, h=120.000000, x=0.000000, y=374.000000 

我从初始到最终看到的唯一区别是viewButtons bounds origin.y,但是如果我尝试设置它,事情会变得混乱 – button突然出现在屏幕的顶部。 button边界足够宽。

我错过了什么?

更新:我错过了一个事实,即我在IB中设置了struts和spring的内部视图,这是与我设置视图框架的代码作斗争。 我删除了代码来设置框架,让IB做这项工作,并摆弄button的偏移量。 不是最漂亮的解决scheme,但我需要释放它。

移动你的button不应该导致你的button的“可点击”区域的修改。 我认为会导致这样的事情发生的唯一原因是:

  1. 旋转后,您可以看到覆盖部分button的透明视图。 如果您具有透明视图,请将其背景颜色设置为红色,然后查看旋转后会发生的情况。

  2. 如果您的button不是主视图的子项,请打开xib文件中的“剪辑子视图”设置,或者在button的父视图的代码中打开clipsToBounds属性。 如果您的button在旋转过程中被剪裁,这可能是您的问题的另一个原因。

让我知道这是否有帮助。

这不是真正的解释为什么你得到这种行为,但你可能会有更好的运气inheritanceUIView,把布局代码放在那里,然后在你的视图控制器中使用uiview子类。 我在我自己的应用程序中对button进行了相当多的animation操作,并且对uiview进行了子类化,这导致了旋转的最小化。

…您的viewButtons.origin.y问题可能与您如何获得屏幕尺寸有关。 也许。 尝试这个

UIView * rootView = [[[UIApplication sharedApplication] keyWindow] rootViewController] .view; CGRect originalFrame = [[UIScreen mainScreen] bounds]; CGRect adjustedFrame = [rootView convertRect:originalFrame fromView:nil];