简体   繁体   English

如何以编程方式自动旋转UIScrollView和UIImageView?

[英]How to autorotate UIScrollView and UIImageView programmatically?

I have a UIScrollView and a UIImageView which I have created programmatically without interface builder. 我有一个UIScrollView和UIImageView,它们是通过编程创建的,没有界面生成器。 How can I make sure both auto-rotate in horizontal orientation? 如何确保两者均沿水平方向自动旋转?

If you're subclassing UIViewController, even programmatically, you should get rotation for free. 如果您要对UIViewController进行子类化(甚至以编程方式进行子类化),则应该免费进行旋转。 Just make sure you set the autoresizingMask on the scroll view and image view so that the mask is set to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight 只需确保在滚动视图和图像视图上设置了autoresizingMask,以便将遮罩设置为UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight if you want the view to adapt to the new screen dimensions. UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight如果您希望视图适应新的屏幕尺寸)。

make sure you have implemented shouldAutorotateToInterfaceOrientation and then do a little 确保已经实现了shouldAutorotateToInterfaceOrientation,然后做一点

yourView.transform = CGAffineTransformIdentity;
yourView.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); 

and using this in your header files can be very useful 并在头文件中使用它会非常有用

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

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

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