[IOS]添加到UITabBar的UIView切换横竖屏

      一般我们可以直接重写 willRotateToInterfaceOrientation方法来实现切换横竖屏时改变控件的位置及内容,但添加到UITabBar里的view是无法触发

willRotateToInterfaceOrientation这个方法的(只有添加到启动窗体windowView中才能触发这个事件)。

  我是采用在承载tabbarcontroll的窗体上响应该方法,去改变每一个tabItem里view的控件按照屏幕方向来调整:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
	//切换横竖屏
	[self setViewFrame:tab1ViewController FrameOrentation:toInterfaceOrientation];
	[self.tab1ViewController UpdateControlByOrentation:toInterfaceOrientation];	
	[self setViewFrame:tab2ViewController FrameOrentation:toInterfaceOrientation];
	[self setViewFrame:tab3ViewController FrameOrentation:toInterfaceOrientation];
}

tab1ViewController里设置具体需要调整的控件:

//切换横竖屏后更新mapview里的控件位置
-(void) UpdateControlByOrentation:(UIInterfaceOrientation)toInterfaceOrientation
{
	CGFloat width=768;
	if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
		width=1024;
	}
	self.resultView.frame=CGRectMake(self.resultView.frame.origin.x,self.resultView.frame.origin.y,self.resultView.frame.size.width, 45.0);
	self.cancelBtn.frame=CGRectMake(50, self.cancelBtn.frame.origin.y, self.cancelBtn.frame.size.width, self.cancelBtn.frame.size.height);
	self.resultLabel.frame=CGRectMake(width/2-self.resultLabel.frame.size.width/2,self.resultLabel.frame.origin.y,self.resultLabel.frame.size.width,self.resultLabel.frame.size.height);
	self.clearBtn.frame=CGRectMake(width-50-self.clearBtn.frame.size.width, self.clearBtn.frame.origin.y, self.clearBtn.frame.size.width, self.clearBtn.frame.size.height);
	
}

网上查到还有一种方法是使用消息,应该也是上一层view在触发rotate事件后发送消息给各个subview,让subview再去旋转:

Observe UIDeviceOrientationDidChangeNotification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

... 

- (void)orientationDidChange:(NSNotification *)note {

NSLog(@"new orientation = %d", [[UIDevice currentDevice] orientation]); 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值