简体   繁体   English

在 Objective-C 中更改屏幕方向时,如何解决日期选择器 UIView 的奇怪行为?

[英]How to fix strange behavior of date picker UIView when changing screen orientation in Objective-C?

I have this project that requires to change the screen orientation of application once the user will pick a date using datePicker .一旦用户使用datePicker选择日期,我有这个项目需要更改应用程序的屏幕方向。 Once the user tapped the Date button, It will show the datePicker The first image is the correct layout of the datePicker when portrait mode.一旦用户点击Date按钮,它将显示datePicker第一个图像是纵向模式时datePicker的正确布局。

Correct Portrait Layout正确的纵向布局正确的纵向布局

When changing the screen orientation from Landscape to Portrait .将屏幕方向从Landscape 更改为 Portrait 时 It will appear the issue.就会出现问题。 Please refer to image below.请参考下图。

The datePicker is located below datePicker 位于下方错误的横向布局

Which is the image below should be the correct layout once turned to landscape.一旦变成横向,下面的图像应该是正确的布局。

正确的横向布局

When you changed the screen orientation back to portrait.当您将屏幕方向更改回纵向时。 The datePicker layout changed. datePicker布局已更改。 Please refer to image below.请参考下图。

Incorrect Portrait Layout不正确的纵向布局

错误的纵向布局

Below are the codes used:以下是使用的代码:

- (void)resizeView:(UIInterfaceOrientation)interfaceOrientation
{
//LOG(@"interfaceOrientation=%ld", (long)interfaceOrientation);
//LOG(@"self.frame =%@", NSStringFromCGRect(self.frame));
//LOG(@"self.bounds=%@", NSStringFromCGRect(self.bounds));

CGFloat x, y, w, h;
viewBack.frame = self.bounds;

#define _HEIGHT_LABEL   (40)
#define _HEIGHT_PICKER  (162)


#define _HEIGHT_TOOLBAR (CGRectGetHeight(toolBar.frame))//(44)
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
    LOG(@"[Portrait]");
    w = CGRectGetWidth(viewBack.frame);
    h = _HEIGHT_LABEL + _HEIGHT_PICKER*2 + _HEIGHT_TOOLBAR;
    viewPicker.frame = CGRectMake(0, 0, w, h);
    viewPicker.center = viewBack.center;

    x = 0;
    y = 0;
    w = CGRectGetWidth(viewPicker.frame);
    h = _HEIGHT_LABEL;
    lblTitle.frame = CGRectMake(x, y, w, h);

    x = 0;
    y = CGRectGetMaxY(lblTitle.frame);
    w = CGRectGetWidth(viewPicker.frame);
    h = _HEIGHT_PICKER;
    datePicker.frame = CGRectMake(x, y, w, h);

    x = 0;
    y = CGRectGetMaxY(datePicker.frame);
    w = CGRectGetWidth(viewPicker.frame);
    h = CGRectGetHeight(datePicker.frame); //datePickerと同じ
    timePicker.frame = CGRectMake(x, y, w, h);

    x = 0;
    y = CGRectGetMaxY(timePicker.frame);
    w = CGRectGetWidth(viewPicker.frame);
    h = _HEIGHT_TOOLBAR;
    toolBar.frame = CGRectMake(x, y, w, h);
    //LOG(@"tool  =%@", NSStringFromCGRect(toolBar.frame));
}
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
    LOG(@"[Landscape]");
    w = CGRectGetWidth(viewBack.frame);
    h = _HEIGHT_LABEL + _HEIGHT_PICKER + _HEIGHT_TOOLBAR;
    viewPicker.frame = CGRectMake(0, 0, w, h);
    viewPicker.center = viewBack.center;

    x = 0;
    y = 0;
    w = CGRectGetWidth(viewPicker.frame);
    h = _HEIGHT_LABEL;
    lblTitle.frame = CGRectMake(x, y, w, h);

    w = CGRectGetWidth(viewPicker.frame) / 2; //画面幅の半分
    x = CGRectGetMidX(viewPicker.frame) - w;
    y = CGRectGetMaxY(lblTitle.frame);
    h = _HEIGHT_PICKER;
    datePicker.frame = CGRectMake(x, y, w, h);

    x = CGRectGetMidX(viewPicker.frame);
    y = CGRectGetMinY(datePicker.frame); //datePickerと同じ
    w = CGRectGetWidth(datePicker.frame); //datePickerと同じ
    h = CGRectGetHeight(datePicker.frame); //datePickerと同じ
    timePicker.frame = CGRectMake(x, y, w, h);

    x = 0;
    y = CGRectGetMaxY(datePicker.frame);
    w = CGRectGetWidth(viewPicker.frame);
    h = CGRectGetHeight(toolBar.frame);//44;
    toolBar.frame = CGRectMake(x, y, w, h);
}

 #if 0
LOG(@"back  =%@", NSStringFromCGRect(viewBack.frame));
LOG(@"base  =%@", NSStringFromCGRect(viewPicker.frame));
LOG(@"image =%@", NSStringFromCGRect(imageViewBlackGraBar.frame));
LOG(@"label =%@", NSStringFromCGRect(lblTitle.frame));
LOG(@"date  =%@", NSStringFromCGRect(datePicker.frame));
LOG(@"time  =%@", NSStringFromCGRect(timePicker.frame));
LOG(@"tool  =%@", NSStringFromCGRect(toolBar.frame));
LOG(@"");
 #endif
}

This is additional information I gathered during my investigation.这是我在调查期间收集的额外信息。 Below is the .xib for portrait and landscape.下面是纵向和横向的.xib

肖像 景观

Try calling [[self view] layoutIfNeeded];尝试调用 [[self view] layoutIfNeeded]; in resizeView在调整大小视图中

or [[self view] setNeedsLayout];或 [[self view] setNeedsLayout]; when orientation occurs.当定向发生时。

use setNeedsLayout if you are okay with the system to update it in the next upcoming update cycle use layoutIfNeeded if you want it to be updated immediately.如果您对系统在下一个即将到来的更新周期中更新它没问题,请使用 setNeedsLayout 如果您希望它立即更新,请使用 layoutIfNeeded。

You should use UITextField instead of UILabel and just set date picker as textField's inputView .您应该使用UITextField而不是UILabel并将日期选择器设置为 textField 的inputView

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM