2016-02-20 185 views
1

我在默认视图的子视图中使用一个视图。我试图将它的高度和宽度设置为与超级视图相同,当它改变方向时。首先,当应用程序加载肖像模式时,此子视图采用完美的高度宽度,但当第二次更改为肖像模式时,它不会更改高度,宽度。 在负载: -当我改变方向我的视图的高度和宽度不会改变

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] 
    addObserver:self selector:@selector(orientationChanged:) 
    name:UIDeviceOrientationDidChangeNotification 
    object:[UIDevice currentDevice]]; 

方法: -

- (void) orientationChanged:(NSNotification *)note 
{ 
    UIDevice * device = note.object; 
    switch(device.orientation) 
    { 
     case UIDeviceOrientationPortrait: 
      NSLog(@"Portrait"); 
      NSLog(@"View Height %f",self.view.frame.size.height); 
      NSLog(@"View Width%f",self.view.frame.size.width); 

      view1.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height); 

      NSLog(@"View1 Height %f",view1.frame.size.height); 
      NSLog(@"View1 Width%f",view1.frame.size.width); 

      break; 

     case UIDeviceOrientationLandscapeRight: 
      NSLog(@" LanScapRight"); 


      view1.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.height, self.view.frame.size.width); 



      break; 
     case UIDeviceOrientationLandscapeLeft: 

      NSLog(@" LandScapLeft"); 

      view1.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height); 


     default: 
      view1.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height); 


      NSLog(@"Default"); 

      //view1.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.height, self.view.frame.size.width); 


      break; 
    }; 
} 

输出:-(问题)

2016-02-20 01:29:15.907 Editing[4999:95632] Portrait 
[![2016-02-20 01:29:15.910 Editing\[4999:95632\] View Height 568.000000 
2016-02-20 01:29:15.910 Editing\[4999:95632\] View Width320.000000 
2016-02-20 01:29:15.911 Editing\[4999:95632\] View1 Height 568.000000 
2016-02-20 01:29:15.912 Editing\[4999:95632\] View1 Width320.000000 
2016-02-20 01:29:39.090 Editing\[4999:95632\] LanScapRight 
2016-02-20 01:29:40.364 Editing\[4999:95632\] Default 
2016-02-20 01:29:41.645 Editing\[4999:95632\] LandScapLeft 
2016-02-20 01:29:41.646 Editing\[4999:95632\] Default 
2016-02-20 01:29:43.316 Editing\[4999:95632\] Portrait 
2016-02-20 01:29:43.317 Editing\[4999:95632\] View Height 320.000000 
2016-02-20 01:29:43.317 Editing\[4999:95632\] View Width568.000000 
2016-02-20 01:29:43.317 Editing\[4999:95632\] View1 Height 320.000000 
2016-02-20 01:29:43.318 Editing\[4999:95632\] View1 Width568.000000] 

enter image description here

enter image description here

+0

你有没有正当理由不使用autolayout? –

+0

smit是你的问题解决? –

+0

@ elio.d是的,我想把日历放在中心。 –

回答

1

由于界面方向的改变,更新用​​户界面是一个相当常见的任务,因此有一个很好的方法可以做到这一点。

解决方案1:

如果您使用的是基于帧的布局(如您的示例代码),建议把你的框架设置代码到视图的layoutSubviews作为贾恩·格雷夫评论。要做到这一点,你必须继承UIView。每当视图边界发生变化时,将调用layoutSubviews方法,因此您不必观察有关接口方向的通知,并且您还可以对其他尺寸变化事件做出反应,例如,多任务分割视图。下面是一个超小例子,如何继承的UIView和实施布点:

// MyView.h

#import <UIKit/UIKit.h> 

@interface MyView : UIView 

@property (strong) UIView* view1; 
@property (strong) UIView* calendar; 

@end 

// MyView.m

#import "MyView.h" 

@implementation MyView 

- (instancetype)init { 
    self = [super init]; 
    if (self) { 
     self.view1 = [[UIView alloc] init]; 
     self.view1.backgroundColor = [UIColor grayColor]; 
     [self addSubview:self.view1]; 
     self.calendar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)]; 
     self.calendar.backgroundColor = [UIColor redColor]; 
     [self.view1 addSubview:self.calendar]; 
    } 
    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    self.view1.frame = self.bounds; 
    self.calendar.center = self.view1.center; 
} 

@end 

如果设置一个MyView的实例到viewcontroller的视图属性它只是工作。

解决方案2:

解决方案1一般可以使用,但在特定情况下可以布点通过autoresizingmasks来完成。 (这里我必须再次向Jan Greve说,他在回答中推荐了这一点。)现在你不需要继承UIView的所有东西都可以从视图控制器处理。你可以插入这样的东西到viewDidLoad

UIView* view1 = [[UIView alloc] initWithFrame:self.view.frame]; 
view1.backgroundColor = [UIColor grayColor]; 
[self.view addSubview:view1]; 
view1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

UIView* calendar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)]; 
calendar.backgroundColor = [UIColor redColor]; 
[view1 addSubview:calendar]; 
calendar.center = view1.center; 
calendar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
+0

你的第二个解决方案是工程..谢谢兄弟..使用这个不需要设置任何方向的方法.. –

+0

但它会隐藏状态栏。 –

+0

在iPhone上以横向模式隐藏状态栏是默认行为(请检查Safari例如)。它独立于视图布局。 – fabe

1
[self.view setNeedsDisplay]; 

在上面的开关盒上使用此代码,此代码用于重新加载 视图,以便您可以获取更新的视图宽度和高度。

+0

这不适用于我,但感谢分享 –

1

如果您只是希望视图具有超级视图的界限而不使用自动布局,则只要超级视图的界限发生变化,具有灵活高度和宽度的自动调整遮罩应该简单地设置正确的帧。

编辑

你的子视图的框架的原点设置为上海华盈的起源 - 我怀疑这不是预期的。如果超视图从(0,0)移开,这会将子视图的原点移动两倍于窗口的移位。

+0

是的,你是正确的自动调整大小从尺寸检查是最好的方式,但我想放置一个日历在此视图的中心。如果我将使用自动调整大小,然后我的calander不会获得中心值。 –

+0

[_calendar setCenter:CGPointMake(_view1.frame.size.width/2,_view1.frame.size.height/2)]; –

+1

这不仅仅是面向变化的问题。外部显示链接怎么样?你应该在'layoutSubviews'中处理你的布局。 – SmokeDispenser

相关问题