如何在ipad中实现uitabbaripad横屏壁纸显示效果

29804人阅读
【MAC/IOS下开发】(58)
ipad横竖屏切换解决方案
& 由于ipad的横竖屏不同,所以好的应用,横竖屏的页面布局也不一样。那么就需要横竖屏的整体解决方案。先看一个横竖屏布局不一样的界面。
上面两张图是来自同一个界面的横竖版的截屏。可以看出,横竖版显示的内容相同,但是界面布局不同。要实现上述布局,主要是运用UIView中 layoutSubviews方法。当UIView设置为自动适配屏幕时,当用户旋转设备的时候,会调用layoutSubviews方法,我们只需重写 这个方法,然后判断用户屏幕的方向。在调整每个空间的位置即可。
下面是实现上述界面的最简单的原型:
首先分析可以知道左面是图片,右面是一个图片加文字的视图。下面就实现一个左面视图右面是一个图加一段字的事例。
事例的截图如下:
其中右面的文字和绿色部分是用一个子视图封装的。
整个布局是我在主视图中添加了一个ContentView视图,在ContentView视图中添加了一个ArticleView视图。
其中ArticleView和ContentView的xib文件都打开了
在ContentView中重写layoutSubviews方法,然后根据stausbar的方向判断当前视图的横竖屏。具体代码:
-(void)layoutSubviews{&
&&& [super layoutSubviews];&
&&& UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation];&
&&& if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {&
&&&&&&& //翻转为竖屏时&
&&&&&&& [self setVerticalFrame];&
&&& }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) {&
&&&&&&& //翻转为横屏时&
&&&&&&& [self setHorizontalFrame];&
-(void)setVerticalFrame&
&&& NSLog(@&竖屏&);&
&&& [titleLable setFrame:CGRectMake(283, 0, 239, 83)];&
&&& [leftView setFrame:CGRectMake(38, 102, 384, 272)];&
&&& [rightView setFrame:CGRectMake(450, 102, 282, 198)];&
-(void)setHorizontalFrame&
&&& NSLog(@&横屏&);&
&&& [titleLable setFrame:CGRectMake(183, 0, 239, 83)];&
&&& [leftView setFrame:CGRectMake(168, 122, 384, 272)];&
&&& [rightView setFrame:CGRectMake(650, 122, 282, 198)];&
在具体的横竖屏方法中,从新设置各个组件的坐标即可。
接下来在ContentView中添加ArticleView视图。
-(id)initWithCoder:(NSCoder *)aDecoder&
&&& if ((self = [super initWithCoder:aDecoder])) {
&&&&&&& NSArray *arrayContentView =[[NSBundle mainBundle] loadNibNamed:@&ArticleView& owner:self options:nil];&
&&&&&&& rightView=[arrayContentView objectAtIndex:0];&
&&&&&&& [self addSubview:rightView];&
由于我用的是xib,所以初始化方法为initWithCoder,在这个中添加新的视图。
同样在ArticleView中设置横竖屏相应空间的坐标即可。
-(void)layoutSubviews{&
&&& [super layoutSubviews];&
&&& UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation];&
&&& CGRect rect=self.&
&&& rect.size.width=282;&
&&& rect.size.height=198;&
&&& [self setFrame:rect];&
&&& if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {&
&&&&&&& //翻转为竖屏时&
&&&&&&& [self setVerticalFrame];&
&&& }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) {&
&&&&&&& //翻转为横屏时&
&&&&&&& [self setHorizontalFrame];&
-(void)setVerticalFrame&
&&& NSLog(@&竖屏&);&
&&& [contentView setFrame:CGRectMake(12, 6, 250, 125)];&
&&& [textLable setFrame:CGRectMake(50, 139, 182, 39)];&
-(void)setHorizontalFrame&
&&& NSLog(@&横屏&);&
&&& [contentView setFrame:CGRectMake(12, 6, 106, 158)];&
&&& [textLable setFrame:CGRectMake(135, 11, 147, 39)];&
&/pre& 需求:程序默认开启是横屏,在压到某个页面的时候是竖屏&p&&/p&&p&& /p&&p&1.默认横屏需要在XX-info.plist&的 Supported&interface&orientations&的第一项为 Landscape&(left&home&button)&/p&&p&2.在切换的竖屏的时候& /p&&p&&/p&&pre&name=&code&&class=&cpp&&-(void)viewWillAppear:(BOOL)animated&&
&&&&UIInterfaceOrientation&orientation&=&[UIApplication&sharedApplication].statusBarO&&
&&&&if&(orientation&!=&UIInterfaceOrientationPortrait&||&&&
&&&&&&&&orientation&!=&UIInterfaceOrientationPortraitUpsideDown)&&
&&&&&&&&[[UIApplication&sharedApplication]&setStatusBarOrientation:UIInterfaceOrientationPortrait];&&
&&&&&&&&&&
&&&&&&&&&&
&&&&&&&&CGFloat&duration&=&[UIApplication&sharedApplication].statusBarOrientationAnimationD&&
&&&&&&&&[UIView&beginAnimations:nil&context:nil];&&
&&&&&&&&[UIView&setAnimationDuration:duration];&&
&&&&&&&&&&
&&&&&&&&&&
&&&&&&&&self.view.transform&=&[self&getTransformMakeRotationByOrientation:orientation];&&
&&&&&&&&[UIView&commitAnimations];&&
-&(CGAffineTransform)getTransformMakeRotationByOrientation:(UIInterfaceOrientation)orientation&&
&&&&if&(orientation&==&UIInterfaceOrientationLandscapeLeft)&&&
&&&&&&&&return&CGAffineTransformMakeRotation(M_PI/2);&&
&&&&else&if&(orientation&==&UIInterfaceOrientationLandscapeRight)&&&
&&&&&&&&return&CGAffineTransformMakeRotation(M_PI/2);&&
&&&&else&if&(orientation&==&UIInterfaceOrientationPortraitUpsideDown)&&
&&&&&&&&return&CGAffineTransformMakeRotation(-M_PI);&&
&&&&else&&&
&&&&&&&&&&
&&&&return&CGAffineTransformI&&
3.返回的到竖屏
-&(void)viewWillDisappear:(BOOL)animated&&
&&&&UIInterfaceOrientation&orientation&=&[UIApplication&sharedApplication].statusBarO&&
&&&&if&(orientation&!=&UIInterfaceOrientationLandscapeLeft&||&&&
&&&&&&&&orientation&!=&UIInterfaceOrientationLandscapeRight)&&
&&&&&&&&[[UIApplication&sharedApplication]&setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];&&
&&&&&&&&&&
&&&&&&&&&&
&&&&&&&&CGFloat&duration&=&[UIApplication&sharedApplication].statusBarOrientationAnimationD&&
&&&&&&&&[UIView&beginAnimations:nil&context:nil];&&
&&&&&&&&[UIView&setAnimationDuration:duration];&&
&&&&&&&&&&
&&&&&&&&self.view.transform&=&CGAffineTransformI&&
&&&&&&&&[UIView&commitAnimations];&&
iPad上的应用一般都会横竖屏支持,所以同一个界面页面的横竖布局也是不一样的。要实现横竖布局的不一样一般用到了UIView的layoutSubView方法。当UIView设置为自动适配屏幕时,当用户设备旋转的时候,就会调用layoutSubView这个方法,只要重写这个方法,然后判断屏幕的方向,调整控件的位址就可以了。现在大家可能会有些疑问,为什么不在UIViewController的这个方法:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
进行判断。因为这个方法是作用于UIViewController下的,而不是直接作用于UIView进行控制,所以会发生延迟,作用的对象也不正确。所以建议用layoutSubView这个方法
使用列子:
-(void)layoutSubviews{&
&&&&[super&layoutSubviews];&
&&&&UIDeviceOrientation interfaceOrientation=[[UIApplication&sharedApplication]statusBarOrientation];&
&&&&if&(interfaceOrientation&==&UIDeviceOrientationPortrait&||&interfaceOrientation&==UIDeviceOrientationPortraitUpsideDown)
&&&&&&&&//翻转为竖屏时&
&&&&&&&&[self&setVerticalFrame];&
&&&&}else&if&(interfaceOrientation==UIDeviceOrientationLandscapeLeft&||&interfaceOrientation&==UIDeviceOrientationLandscapeRight)
&&&&&&&&//翻转为横屏时&
&&&&&&&&[self&setHorizontalFrame];&
-(void)setVerticalFrame&
&&&&NSLog(@&竖屏&);&
&&&&[titleLable&setFrame:CGRectMake(283,&0,&239,&83)];&
&&&&[leftView&setFrame:CGRectMake(38,&102,&384,&272)];&
&&&&[rightView&setFrame:CGRectMake(450,&102,&282,&198)];&
-(void)setHorizontalFrame&
&&&&NSLog(@&横屏&);&
&&&&[titleLable&setFrame:CGRectMake(183,&0,&239,&83)];&
&&&&[leftView&setFrame:CGRectMake(168,&122,&384,&272)];&
&&&&[rightView&setFrame:CGRectMake(650,&122,&282,&198)];&
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:740427次
积分:6865
积分:6865
排名:第3511名
原创:75篇
转载:197篇
评论:63条
(1)(4)(3)(1)(1)(2)(2)(11)(4)(1)(3)(1)(3)(1)(2)(1)(2)(4)(9)(8)(15)(6)(13)(15)(7)(5)(1)(2)(22)(19)(20)(3)(1)(2)(4)(5)(20)(10)(9)(3)(12)(14)(1)(1)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'主题 : [急]MBProgressHUD 如何在iPad横屏时控制方向
级别: 精灵王
UID: 19391
可可豆: 2486 CB
威望: 3776 点
在线时间: 1840(时)
发自: Web Page
[急]MBProgressHUD 如何在iPad横屏时控制方向&&&
如题,在iPad横屏时,它的小黑框会乱转或者是横躺着的,这个怎么办?- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation调用这个的时候,interfaceOrientation总是等于UIInterfaceOrientationPortrait;后来说要-(NSUInteger)supportedInterfaceOrientations,但是系统又不调用,求解~~[UIApplication sharedApplication].statusBarOrientation 用来获取当前设备的方向,可是明明是横屏的,出来的值是视屏的。[ 此帖被ztdj121在 18:37重新编辑 ]
级别: 新手上路
可可豆: 68 CB
威望: 68 点
在线时间: 13(时)
发自: Web Page
回 楼主(ztdj121) 的帖子
大大,这个问题如何解决呢?
级别: 新手上路
可可豆: 14 CB
威望: 14 点
在线时间: 76(时)
发自: Web Page
我也想知道这个问题到底应该怎么解决&&请问你们都解决好了吗?
级别: 新手上路
可可豆: 14 CB
威望: 14 点
在线时间: 76(时)
发自: Web Page
回 1楼(hrj) 的帖子
你这个现在弄好了吗?
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
观乎其上,得乎其中,
观乎其中,得乎其下。
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
找网上找了七八种办法,一一试了,要么没效果,要么不能完美解决,最后用下面的方法解决了:个人觉得这个问题没有那么复杂,试试下面两个方法:&1、在对应的 ViewController 中设置 self.wantsFullScreenLayout = YES。&2、在 xib 中设置 statusbarstyle 为 none;第一种可以解决原帖地址:其中&的评论
阅读(366)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'UITabbarController在iPad中向下偏移问题解决办法',
blogAbstract:'找网上找了七八种办法,一一试了,要么没效果,要么不能完美解决,最后用下面的方法解决了:个人觉得这个问题没有那么复杂,试试下面两个方法:&1、在对应的 ViewController 中设置 self.wantsFullScreenLayout = YES。&',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:5,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'观乎其上,得乎其中,\n观乎其中,得乎其下。',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}主题 : 请教iPhone应用在iPad上横屏显示的问题?(结贴)
级别: 精灵王
发帖: 1517
可可豆: 5147 CB
威望: 5418 点
在线时间: 1079(时)
发自: Web Page
来源于&&分类
请教iPhone应用在iPad上横屏显示的问题?(结贴)&&&
iphone应用装在iPad上竖屏幕上好的,横屏在iPad上显示有问题,界面被切成了2部分,中间一条大黑线,我感觉是iOS8的问题,iPhone应用在iPad上就这个逼样,不知道各位遇到过没?这个界面代码是写成了只允许竖屏的,横屏的时候,模拟器上显示正常,但是真机中间压缩了。已经提交审核了,担心审核出岔子。请看4楼[ 此帖被xiaochengfei在 10:14重新编辑 ]
图片:11.jpg
图片:12.jpg
级别: 精灵王
发帖: 1517
可可豆: 5147 CB
威望: 5418 点
在线时间: 1079(时)
发自: Web Page
&&没人知道吗?。。。
级别: 新手上路
可可豆: 2 CB
威望: 2 点
在线时间: 0(时)
发自: Web Page
楼主节哀,话说你是怎么做到的
级别: 精灵王
发帖: 1517
可可豆: 5147 CB
威望: 5418 点
在线时间: 1079(时)
发自: Web Page
痛苦&&,stackoverflow上问了也没人知道&& &&
级别: 精灵王
发帖: 1517
可可豆: 5147 CB
威望: 5418 点
在线时间: 1079(时)
发自: Web Page
收贴, 苹果的人承认是“有趣的现象”==bug ,郁闷。
图片:1.jpg
级别: 新手上路
可可豆: 13 CB
威望: 3 点
在线时间: 94(时)
发自: Web Page
你好,请问你解决了吗?我也遇到了同样的问题
级别: 精灵王
发帖: 1517
可可豆: 5147 CB
威望: 5418 点
在线时间: 1079(时)
发自: Web Page
回 5楼(jamehery) 的帖子
无法解决,iOS8的bug,可能苹果在后期版本会修复。。。
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
iOS 中,一个 UITabBarController 管理着四个 UINavigationController,且每个 Navigation 都有自己的根视图,请问如何实现点击标签一时 UINavigationController1 的屏幕方向为 UpSideDown,点击标签二时 UINavigationController2 的方向为 LandScapeLeft???
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
以下方法仅对deploy target大于等于iOS6的工程有效,如果题主的应用需要支持iOS5(默哀),请pass。
在info.plist中设置方向,包含你需要的所有方向,以题中意,UpSideDown和LandScapeLeft;
继承UITabBarController,override以下三个方法
- (BOOL)shouldAutorotate
return [self.selectedViewController shouldAutorotate];
- (NSUInteger)supportedInterfaceOrientations
return [self.selectedViewController supportedInterfaceOrientations];
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
继承UINavigationController,override和UITabBarController中相同的方法,将selectedViewController改为topViewController
- (BOOL)shouldAutorotate
return [self.topViewController shouldAutorotate];
- (NSUInteger)supportedInterfaceOrientations
return [self.topViewController supportedInterfaceOrientations];
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
return [self.topViewController preferredInterfaceOrientationForPresentation];
在真正实现界面的ViewController里,override上面这三个方法,override规则如下:
preferredInterfaceOrientationForPresentation表示viewController初始显示时的方向;
supportedInterfaceOrientations是在该viewController中支持的所有方向;
shouldAutorotate表示是否允许旋屏。
首先,对于任意一个viewController,iOS会以info.plist中的设置和当前viewController的preferredInterfaceOrientationForPresentation和supportedInterfaceOrientations三者支持的方法做一个交运算,若交集不为空,则以preferredInterfaceOrientationForPresentation为初始方向,交集中的所有方向均支持,但仅在shouldAutorotate返回YES时,允许从初始方向旋转至其他方向。若交集为空,进入viewController时即crash,错误信息中会提示交集为空。
其次,UINavigationController稍有些特别,难以用常规API做到同一个naviVC中的ViewController在不同方向间自如地切换。(如果去SO之类的地方搜索,会找到一个present empty viewController and then dismiss it之类的hacky trick,不太建议使用),如果要在横竖屏间切换,建议使用presentXXX方法。
再次,AppDelegate中有一个委托方法可以动态的设置应用支持的旋转方向,且此委托的返回值会覆盖info.plist中的固定设置。使用该方法的便利之处不言自明,但缺点是搞明白当前哪个ViewController即将要被显示,很可能会导致耦合增加;
最后,以上均为个人在iOS8 SDK下得到的实践结果,请题主结合工程实际参考使用。
同步到新浪微博
分享到微博?
Hi,欢迎来到 SegmentFault 技术社区!⊙▽⊙ 在这里,你可以提出编程相关的疑惑,关注感兴趣的问题,对认可的回答投赞同票;大家会帮你解决编程的问题,和你探讨技术更新,为你的回答投上赞同票。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:

我要回帖

更多关于 ipad竖屏横屏怎么调 的文章

 

随机推荐