iOS橫豎屏解決方案
ios橫豎屏的效果是不相同的,所以我們?cè)陂_(kāi)發(fā)中如果允許屏幕橫豎屏間的切換,那么我們就要調(diào)整視圖的布局。利用Interface Builder開(kāi)發(fā),我們可以快速的拖拽出合適的界面布局,但是屏幕自動(dòng)切換布局不能很好的適配,下圖是,沒(méi)有做任何調(diào)整的狀態(tài)下,實(shí)現(xiàn)的橫豎屏切換,可以看到界面不是很美觀(guān)。
目前我所知的實(shí)現(xiàn)ios橫豎屏切換的解決方案共有三種:
1.利用Interface Builder適配器自動(dòng)適配調(diào)整界面。
2.在橫豎屏切換時(shí),每個(gè)控件重新布局。
3.利用Interface Builder創(chuàng)建兩個(gè)視圖,橫屏?xí)r切換到橫屏視圖,豎屏?xí)r切換到豎屏視圖。
在ios中,橫豎屏切換時(shí),會(huì)調(diào)用下面函數(shù):
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
- //zuo
- }
- if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
- //you
- }
- if (interfaceOrientation==UIInterfaceOrientationPortrait) {
- //shang
- }
- if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
- //xia
- }
- return YES;
- }
返回yes表示切換屏幕,返回no是不能向相應(yīng)的方向切換視圖。
下面分別介紹一下三種方法,***種方法最簡(jiǎn)單,但是效果是最差的,我們只需用Interface bulider修改相應(yīng)的屬性即可。實(shí)現(xiàn)的效果如下:
實(shí)現(xiàn)的方法:
選中控件,按command+3,上圖紅框部分的紅線(xiàn)表示距離不能自動(dòng)適配,要是虛線(xiàn)表示距離可以自動(dòng)適配。我們選擇可以自動(dòng)適配,***的結(jié)果就如上圖。
第二種方法:
第二種方法是相應(yīng)的控件和代碼相關(guān)聯(lián):
代碼:
- @interface ipad_demooViewController : UIViewController {
- IBOutlet UIButton *myButton1;
- IBOutlet UIButton *myButton2;
- IBOutlet UIButton *myButton3;
- IBOutlet UIButton *myButton4;
- IBOutlet UIButton *myButton5;
- IBOutlet UIButton *myButton6;
- }
- @property (nonatomic,retain) UIButton *myButton1;
- @property (nonatomic,retain) UIButton *myButton2;
- @property (nonatomic,retain) UIButton *myButton3;
- @property (nonatomic,retain) UIButton *myButton4;
- @property (nonatomic,retain) UIButton *myButton5;
- @property (nonatomic,retain) UIButton *myButton6;
- @end
和IB相關(guān)聯(lián):
更改每一個(gè)控件的布局:
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
- //zuo
- self.myButton1.frame=CGRectMake(86, 208, 72, 37);
- self.myButton2.frame=CGRectMake(480, 208, 72, 37);
- self.myButton3.frame=CGRectMake(86, 308, 72, 37);
- self.myButton4.frame=CGRectMake(480, 308, 72, 37);
- self.myButton5.frame=CGRectMake(86, 408, 72, 37);
- self.myButton6.frame=CGRectMake(480, 408, 72, 37);
- }
- if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
- //you
- self.myButton1.frame=CGRectMake(86, 208, 72, 37);
- self.myButton2.frame=CGRectMake(480, 208, 72, 37);
- self.myButton3.frame=CGRectMake(86, 308, 72, 37);
- self.myButton4.frame=CGRectMake(480, 308, 72, 37);
- self.myButton5.frame=CGRectMake(86, 408, 72, 37);
- self.myButton6.frame=CGRectMake(480, 408, 72, 37);
- }
- if (interfaceOrientation==UIInterfaceOrientationPortrait) {
- //shang
- self.myButton1.frame=CGRectMake(86, 208, 72, 37);
- self.myButton2.frame=CGRectMake(480, 208, 72, 37);
- self.myButton3.frame=CGRectMake(86, 308, 72, 37);
- self.myButton4.frame=CGRectMake(480, 308, 72, 37);
- self.myButton5.frame=CGRectMake(86, 408, 72, 37);
- self.myButton6.frame=CGRectMake(480, 408, 72, 37);
- }
- if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
- //xia
- self.myButton1.frame=CGRectMake(86, 208, 72, 37);
- self.myButton2.frame=CGRectMake(480, 208, 72, 37);
- self.myButton3.frame=CGRectMake(86, 308, 72, 37);
- self.myButton4.frame=CGRectMake(480, 308, 72, 37);
- self.myButton5.frame=CGRectMake(86, 408, 72, 37);
- self.myButton6.frame=CGRectMake(480, 408, 72, 37);
- }
- return YES;
- }
第三種方法是創(chuàng)建兩個(gè)視圖,下面看一下實(shí)現(xiàn)過(guò)程:
首先創(chuàng)建兩個(gè)視圖:
- IBOutlet UIView *hView;
- IBOutlet UIView *vView;
創(chuàng)建相應(yīng)的@property方法.
然后在IB中在復(fù)制一個(gè)view。
把一個(gè)視圖做橫屏?xí)r的布局,一個(gè)view做豎屏?xí)r的布局。把相應(yīng)的view和相應(yīng)的方法相連接,在設(shè)置一個(gè)默認(rèn)視圖為view。
下面就是代碼實(shí)現(xiàn):
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
- //zuo
- self.view=self.hView;
- }
- if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
- //you
- self.view=self.hView;
- }
- if (interfaceOrientation==UIInterfaceOrientationPortrait) {
- //shang
- self.view=self.vView;
- }
- if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
- //xia
- self.view=self.vView;
- }
- return YES;
- }
實(shí)現(xiàn)的效果如下:
上述就是我目前知道的三種橫豎屏解決方案,我們可以看到第三種比較簡(jiǎn)單,但是編寫(xiě)比較麻煩,實(shí)現(xiàn)復(fù)雜邏輯比較麻煩,第二種方法實(shí)現(xiàn)起來(lái)不直觀(guān),調(diào)試比較麻煩,但是效果***。
源代碼:http://easymorse-iphone.googlecode.com/svn/trunk/ipad.demoo/
ios6.0橫豎屏切換問(wèn)題解決
this class is not key value coding-compliant for the key
ios5里面的旋轉(zhuǎn)方法ios6里面確實(shí)掉不到了,但是還是可以用的。
首先,在app的主界面(也就是自己的主ViewController.m)里面加上
- -(NSUInteger)supportedInterfaceOrientations{
- return UIInterfaceOrientationMaskAllButUpsideDown;//這里返回哪個(gè)值,就看你想支持那幾個(gè)方向了。這里必須和后面plist文件里面的一致(我感覺(jué)是這樣的)。
- }
- - (BOOL)shouldAutorotate {
- return YES;//支持轉(zhuǎn)屏
- }
這兩個(gè)函數(shù)。
然后在plist文件里面找到Supported interface orientations (iPad)選項(xiàng),添加你想支持的方向,都有提示的。
然后問(wèn)題就解決了。
也許我描述的還有問(wèn)題,希望你能指正。謝謝了。
- -(NSUInteger)supportedInterfaceOrientations{
- return UIInterfaceOrientationMaskAllButUpsideDown;//這里返回哪個(gè)值,就看你想支持那幾個(gè)方向了。這里必須和后面plist文件里面的一致(我感覺(jué)是這樣的)。
- }
這里的設(shè)置會(huì)覆蓋掉plist中的值
還有需要注意:mainViewController要設(shè)置為window的rootViewController,addSubView上去可能存在問(wèn)題。并且上面的所有subViewController都會(huì)受到rootViewController支持朝向的影響