簡體   English   中英

如何在不更改iPhone應用程序中的設備方向的情況下更改應用程序的方向

[英]How to change the orientation of the app without changing the device orientation in iphone app

我想更改應用程序的方向而不更改iPhone應用程序中的設備方向。 我想以編程方式將視圖從portraid模式更改為landscap模式。

而且還想知道這將被蘋果​​商店接受嗎?

謝謝

現在我從其他人那里得到了以下解決方案

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

當您在那時添加此行時,會出現一個警告,要刪除此警告,只需在實現文件中添加以下代碼。

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)


    - (void) setOrientation:(UIInterfaceOrientation)orientation;


    @end

然后在波紋管方法中,如果需要,只需編寫此代碼。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    {

        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);

    }

但是現在想知道這是否被蘋果應用商店接受了嗎?

謝謝

使用此行以編程方式更改方向...

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

並且在那時添加此行時,會出現一個警告,要刪除此警告,只需在實現文件中添加以下代碼。

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end

然后在波紋管方法中,如果需要,只需編寫此代碼。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //    return NO;
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

希望對您有幫助。

:)

添加一個類變量

Bool isInLandsCapeOrientation;

在viewDidLoad中

將此標志設置為

isInLandsCapeOrientation = false;

添加以下功能

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     if (!isInLandsCapeOrientation) {
         return (UIInterfaceOrientationIsPortrait(interfaceOrientation));

     }else {
         return  (UIInterfaceOrientationIsLandscape(interfaceOrientation));
     }
}

要將方向從縱向更改為橫向,請通過按鈕操作進行

 - (IBAction)changeOrientationButtonPressed:(UIButton *)sender
{
    isInLandsCapeOrientation = true;
    UIViewController *viewController = [[UIViewController alloc] init];
    [self presentModalViewController:viewController animated:NO];
    [self dismissModalViewControllerAnimated:NO];
}

這對我來說很好。

要將定向寫意模式更改為橫向模式,請使用此代碼

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

使用此代碼以編程方式更改方向...

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

該文檔將方向屬性描述為只讀,因此,如果它可以工作,我不確定您將來是否可以依靠它來工作(除非Apple做得很聰明,並且對此進行了更改;無論方向如何變化,都強制改變方向)用戶當前正在握住他們的設備,這是一項顯而易見的功能需求)。

另外,插入到viewDidLoad中的以下代碼將成功(有點奇怪)強制方向(假設您已經修改過,應該是AutorotateToInterfaceOrientation):

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];
    [view removeFromSuperview];
    [window addSubview:view];
}

顯然,如果用戶當前以縱向方向握住其設備,則可以執行此操作(因此,大概應該將您的shouldAutorotateToInterfaceOrientation設置為僅橫向使用,如果用戶以縱向模式握住其設備,則此例程會將其轉換為橫向)。 如果您的shouldAutorotateToInterfaceOirentation僅設置為縱向,則只需將UIDeviceOrientationIsPortraitUIDeviceOrientationIsLandscape交換即可。

出於某種原因,從主窗口中刪除該視圖,然后重新添加該視圖會強制其查詢shouldAutorotateToInterfaceOrientation並正確設置方向。 鑒於這不是Apple認可的方法,也許應該避免使用它,但是它對我有用。 你的旅費可能會改變。 但這也涉及其他技術。 檢查SO討論

如果您只想更改橫向的特定視圖,則可以在其viewDidLoad中嘗試以下操作

    float   angle = M_PI / 2; 
    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
   [ [self.view] setTransform:transform];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM