歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 如何控制iPad/iPhone橫向和豎向詳解

如何控制iPad/iPhone橫向和豎向詳解

日期:2017/3/1 10:22:55   编辑:Linux編程

對於剛接觸ipad/iPhone編程的人來說,控制其顯示方向是挺糾結的!下面我來給大家詳細講講這應該是如何控制的。

相信很多人都是,我要控制其橫向顯示不是很簡單,直接在代碼裡的

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

函數裡控制其顯示方向不就行了?並且網上也有很多這方面的教程也是這麼說的!


那好,那麼我們新建一個工程,在其AppDelegate.m裡添加下面的代碼

控制橫向【在網上搜索,相信大家都能得到如何代碼控制其橫向顯示效果的代碼】

  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  2. {
  3. if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
  4. {
  5. return YES;
  6. }
  7. else {
  8. return NO;
  9. }
  10. }

大家可以在這裡下載這個工程:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/5月/14日/如何控制iPad&iPhone橫向和豎向詳解/

當然,也可以自己寫,畢竟這個也不難

然後在ViewController.m裡添加一張圖片:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. UIImageView * test = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sleep.jpg"]];
  6. [self.view addSubview:test];
  7. }
好了,那現在我們來看看其顯示效果吧!

當我在虛擬機裡讓設備是豎向時,其顯示效果是:


當屏幕是橫向時,顯示效果如下:



顯然,橫向時的效果是我們想要的,但是,當屏幕豎向時,為什麼圖片是橫向的,但是,顯示屏的下面是白色的,這肯定不是我們想要的效果。那我們該怎麼辦呢?

Copyright © Linux教程網 All Rights Reserved