viewDidLayoutSubviews:和UIInterfaceOrientationIsPortrait

时间:2014-12-19 11:26:38

标签: ios objective-c

我不得不问这个问题,因为我对这个问题的机制完全感到困惑。我正在尝试检查纵向方向,虽然它在我的情况下在viewdidload中正常工作,因为我只需要拨打一次电话,我仍然想知道如果每次设备被转回时需要检查画像该怎么办画像。

我有以下代码

-(void)viewDidLayoutSubviews{
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
    self.adviceLabel.preferredMaxLayoutWidth = [[UIScreen mainScreen]bounds].size.width-25;
    [self.adviceLabel setFrame:CGRectMake(0, 0,[[UIScreen mainScreen]bounds].size.width-25 , 103)];
    NSLog(@"portrait");
}

首先"肖像"被爱3次,我想这是因为我的VC中有3个观点,呃。但是当我开始转动屏幕时,它实际上开始记录"肖像"当我转向风景时#34;意味着它在我点击横向模式后记录。这背后有什么意义吗?我讨厌离开一个主题而不理解。提前谢谢!

1 个答案:

答案 0 :(得分:0)

使用以下代码实现目标:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
 // do something before rotation
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
 // do something after rotation
}
每次视图及其子视图更改其帧时,都会调用

-viewDidLayoutSubviews方法。每次视图更改框架时,检查方向都不是好习惯。