【问题标题】:iOS button tap area after device rotation设备旋转后的 iOS 按钮点击区域
【发布时间】:2013-04-19 16:02:40
【问题描述】:

旋转后我遇到了几个按钮问题。在纵向模式下,按钮堆叠在屏幕底部。在横向时,它们会被移动并调整大小以在屏幕底部并排显示。这一切都很好,按钮行为正确。但是,如果手机随后旋转回纵向,则按钮的点击区域是错误的 - 只有左侧 1/3 的按钮是可点击的。

这是执行旋转的代码。 (我也在 viewWillAppear 中调用它,因此它总是在显示视图时执行,以防视图已经处于横向状态。)我已将按钮放在它们自己的内部视图中。

- (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;
}

我添加了一些日志记录以确定旋转结束时帧和边界的情况:

 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,但是如果我尝试设置它,事情就会变得一团糟——按钮突然出现在屏幕顶部。按钮边界足够宽。

我错过了什么?

更新:我错过了在 IB 中为封闭视图设置 struts 和弹簧的事实,这与我设置视图框架的代码冲突。我删除了设置框架的代码,让 IB 完成工作,并调整了按钮的偏移量。不是最漂亮的解决方案,但我需要发布它。

【问题讨论】:

    标签: ios uibutton screen-rotation


    【解决方案1】:

    移动按钮不应导致修改按钮的“可点击”区域。我认为会导致这种情况发生的唯一原因是:

    1. 你有一个透明的视图覆盖你的按钮后 回转。如果您确实有透明视图,请制作它们的背景 涂成红色,看看旋转后会发生什么。

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

    如果有任何帮助,请告诉我。

    【讨论】:

    • 是的,这确实有帮助,谢谢。我在按钮的封闭视图上设置了支柱和弹簧,我的代码正在与 IB 争夺控制权 - 封闭视图正在切断按钮的点击区域,即使它们是可见的。
    • 是的……我认为这是问题所在……我已经发生过几次了。很高兴我能帮上忙。
    【解决方案2】:

    这并不能真正解释为什么您会出现这种行为,但您可能会更幸运地将 UIView 子类化,将布局代码放在那里,然后在视图控制器中使用 UIView 子类。我在自己的应用程序中为按钮设置了相当多的动画,并且子类化 UIView 导致在旋转方面的头疼最少。

    … 您的 viewButtons.origin.y 问题可能与您获取屏幕尺寸的方式有关。也许。试试这个

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

    【讨论】:

    • 谢谢,我将使用此代码而不是获取屏幕尺寸的代码。
    猜你喜欢
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多