iphone - 修复 uiview 在 iphone sdk 中的位置

标签 iphone objective-c ios uiinterfaceorientation

在我的应用程序中,我想根据 iPhone 的主页按钮方向设置 UIView 方向。我使用以下方式完成了它:

/* I handled view orientation in shouldAutorotate method*/
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
            viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
            viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        else if (interfaceOrientation == UIInterfaceOrientationPortrait)
            viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
        else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }  

其中 viewForBarButtons 是 viewController 内的 UIView。

但是如果我设置 return Yes 而不是 return (interfaceOrientation == UIInterfaceOrientationPortrait);那么它就不起作用了。

如何解决这个问题。如果有人知道的话,请帮助我。 LifeCards 应用程序中也实现了类似的功能。 提前致谢。

最佳答案

上面的方法是当设备方向改变时决定是否将 View 旋转到特定方向。

return (interfaceOrientation == UIInterfaceOrientationPortrait);

指定仅支持纵向。

return YES;

支持所有方向。

但是,如果您将 View Controller 放入 TabBarController 中,那么仅当所有 View Controller 都支持该特定方向时,它才会旋转。

不要将上面的代码放在 willRotateToInterfaceOrientation 中自动调整对象的大小。

但是你需要知道一件事,通过指定

UIViewAutoresizingFlexibleLeftMargin

您只是指定 View 可以在对象和左边距之间放置尽可能多的空间。但在方向更改期间,它会保留其他侧的先前位置,因此您可能需要在物理上更改对象的原点(viewForBarButtons)

好的。我猜你的意思是你想要 viewForBarButtons 位于 homeButton 旁边,并且需要相应地定位/旋转其 subview 。

首先注册 devie Rotaion 或使用 didRotateInterfaceOrientation 来启动 viewForBarButtons 的旋转 subview 。

#define degreesToRadians(x) (M_PI * x / 180.0)

旋转 subview :用 subview 对象替换 self

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (animated)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
    }

    if (orientation == UIInterfaceOrientationPortraitUpsideDown)
        self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(180)); 

    else if (orientation == UIInterfaceOrientationLandscapeRight)
        self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(90));  

    else if (orientation == UIInterfaceOrientationLandscapeLeft)
        self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(-90));
    else 
        self.transform=CGAffineTransformIdentity;

    if (animated)
        [UIView commitAnimations];

关于iphone - 修复 uiview 在 iphone sdk 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312849/

相关文章:

ios - 字体名称 :size: How to import own font to project?

iOS - 使用 AFNetworking 执行同步 HTTP 请求

iphone - 将日期和时间组合成单个 NSDate 的问题

iphone - AVAudioPlayer如何反转播放?

iphone - 如何使按钮的文字向左对齐?

iphone - 如何更改默认 View 大小

iphone - 根据多个属性对NSArray进行排序

iphone - 如何使用 UIBezierPath 在 Cocoa Touch 中绘制多边形

objective-c - 如何使用 UIAlertController 修复运行时错误

ios - NSTimer 不调用方法