我想调整工具栏的大小

发布于 2024-11-08 11:07:57 字数 3284 浏览 3 评论 0原文

我想在方向改变时调整工具栏的大小。 但我遇到了一个难题。

我有两个视图控制器。 第一个视图控制器有一个按钮。当触摸此按钮时,显示屏上将出现第二个视图。这些视图控制器属于导航控制器。

第一个视图控制器使导航栏显示。第二个视图控制器使导航栏隐藏。

第一个视图控制器中的重要方法

- ( void ) viewWillAppear: ( BOOL )animated {

      [ super viewWillAppear: animated ];
      [ [ [ self navigationController ] navigationBar ] setHidden: NO ];

      UIButton *nextButton;
      nextButton = [ UIButton buttonWithType: UIButtonTypeRoundedRect ];
      [ nextButton setFrame: CGRectMake( 50.0, 50.0, 200.0, 150.0 ) ];
      [ nextButton setTag: 100 ];
      [ nextButton addTarget: self action: @selector( touchedNextButton: ) forControlEvents: UIControlEventTouchUpInside ];
      [ [ self view ] addSubview: nextButton ];
}

- ( BOOL ) shouldAutorotateToInterfaceOrientation: ( UIInterfaceOrientation )interfaceOrientation {

   // Return YES for supported orientations
   BOOL iResult;
   iResult = ( ( interfaceOrientation == UIInterfaceOrientationPortrait ) || ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) ||
                       ( interfaceOrientation == UIInterfaceOrientationLandscapeRight ) );
   return iResult;

}

    - ( void ) didRotateFromInterfaceOrientation: ( UIInterfaceOrientation )fromInterfaceOrientation {

        UIInterfaceOrientation orientation = [ [ UIDevice currentDevice ] orientation ];

        UIButton *button = ( UIButton * )[ [ self view ] viewWithTag: 100 ];

        switch ( orientation ) {
            case UIInterfaceOrientationPortrait:
                [ button setFrame: CGRectMake( 50.0, 50.0, 200.0, 150.0 ) ];
                break;
            case UIInterfaceOrientationLandscapeLeft:
            case UIInterfaceOrientationLandscapeRight:
                [ button setFrame: CGRectMake( 50.0, 50.0, 200.0, 150.0 ) ];
                break;
            default:
                break;
        }
    }

第二个视图控制器中的重要方法

- ( void ) viewDidLoad {

    [ [ [ self navigationController ] navigationBar ] setHidden: YES ];

    UIImage *imageBackArrow_iPhone = [ UIImage nonCachedImageNamed: @"backArrow_iPhone.png" ];
    NSMutableArray *toolbarItems = [ [ NSMutableArray alloc ] initWithCapacity: 5 ];
    [ toolbarItems addObject: [ [ [ UIBarButtonItem alloc ] initWithImage: imageBackArrow_iPhone style: UIBarButtonItemStylePlain target: self
                                                                       action: @selector( touchedBackArrowButton ) ] autorelease ] ];
        UIToolbar *toolbar = [ [ UIToolbar alloc ] initWithFrame: CGRectMake( 0.0, 416.0, 320.0, 44.0 ) ];
        [ toolbar setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin ];
        [ toolbar setItems: toolbarItems ];
        [ toolbarItems release ];
        [ [ self view ] addSubview: toolbar ];
        [ toolbar release ];
    }
}

第二个视图控制器中的其余方法与第一个视图控制器类似。

在此处输入图像描述

纵向模式后,我触摸了下一步按钮。改变方向效果很好。 (第一个屏幕截图)

但是从第一个视图控制器中的纵向模式开始,我更改为横向模式。 然后我触摸了下一个按钮,这造成了奇怪的布局。 (第二个屏幕截图)

我不知道是什么造成了这个问题。

请让我知道您的经历。你的建议会让我清醒。 感谢您的阅读。

I want to resize my toolbar when orientation changes.
But I have faced a difficult problem.

I have two view controllers.
The first view controller have a button. When this button touches, the second view will appear on display. These view controllers belong to a navigation controller.

The first view controller make navigation bar show. And second view controller make navigation bar hide.

important methods in the first view controller

- ( void ) viewWillAppear: ( BOOL )animated {

      [ super viewWillAppear: animated ];
      [ [ [ self navigationController ] navigationBar ] setHidden: NO ];

      UIButton *nextButton;
      nextButton = [ UIButton buttonWithType: UIButtonTypeRoundedRect ];
      [ nextButton setFrame: CGRectMake( 50.0, 50.0, 200.0, 150.0 ) ];
      [ nextButton setTag: 100 ];
      [ nextButton addTarget: self action: @selector( touchedNextButton: ) forControlEvents: UIControlEventTouchUpInside ];
      [ [ self view ] addSubview: nextButton ];
}

- ( BOOL ) shouldAutorotateToInterfaceOrientation: ( UIInterfaceOrientation )interfaceOrientation {

   // Return YES for supported orientations
   BOOL iResult;
   iResult = ( ( interfaceOrientation == UIInterfaceOrientationPortrait ) || ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) ||
                       ( interfaceOrientation == UIInterfaceOrientationLandscapeRight ) );
   return iResult;

}

    - ( void ) didRotateFromInterfaceOrientation: ( UIInterfaceOrientation )fromInterfaceOrientation {

        UIInterfaceOrientation orientation = [ [ UIDevice currentDevice ] orientation ];

        UIButton *button = ( UIButton * )[ [ self view ] viewWithTag: 100 ];

        switch ( orientation ) {
            case UIInterfaceOrientationPortrait:
                [ button setFrame: CGRectMake( 50.0, 50.0, 200.0, 150.0 ) ];
                break;
            case UIInterfaceOrientationLandscapeLeft:
            case UIInterfaceOrientationLandscapeRight:
                [ button setFrame: CGRectMake( 50.0, 50.0, 200.0, 150.0 ) ];
                break;
            default:
                break;
        }
    }

important methods in the second view controller

- ( void ) viewDidLoad {

    [ [ [ self navigationController ] navigationBar ] setHidden: YES ];

    UIImage *imageBackArrow_iPhone = [ UIImage nonCachedImageNamed: @"backArrow_iPhone.png" ];
    NSMutableArray *toolbarItems = [ [ NSMutableArray alloc ] initWithCapacity: 5 ];
    [ toolbarItems addObject: [ [ [ UIBarButtonItem alloc ] initWithImage: imageBackArrow_iPhone style: UIBarButtonItemStylePlain target: self
                                                                       action: @selector( touchedBackArrowButton ) ] autorelease ] ];
        UIToolbar *toolbar = [ [ UIToolbar alloc ] initWithFrame: CGRectMake( 0.0, 416.0, 320.0, 44.0 ) ];
        [ toolbar setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin ];
        [ toolbar setItems: toolbarItems ];
        [ toolbarItems release ];
        [ [ self view ] addSubview: toolbar ];
        [ toolbar release ];
    }
}

The rest methods in second view controller are similar to the first view contoller.

enter image description here

enter image description here

After starting on portrait mode in the first view controller, I touched the next button. Changing Orientations work fine.
(The first screen shot)

But starting on portrait mode in the first view controller, I changed to landscape mode.
Then I touched the next button, this made weird layout.
(The second screen shot)

I don't know what makes this problem.

Please let me know your experiences. Your advices will make me awake.
Thank you for your reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月如刀 2024-11-15 11:07:57

该代码看起来不太可能......但是
您也可以尝试在视图控制器的 viewWillAppear 方法中调整内容的大小。
这可能会有所帮助。

The code looks very unlikely.... but
you can try resizing stuff in your viewWillAppear Method of your viewcontroller as well.
This could help.

盗心人 2024-11-15 11:07:57

为什么不直接使用自动调整大小蒙版呢?

Why don't you just use autoresizing masks?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文