[英]When i changing orientation my view's height and width are not changed
我在默认视图的子视图中使用了一个视图。 我试图将其高度和宽度设置为与超级视图相同,当它改变方向时。首先,当应用程序加载纵向模式时,此子视图采用完美的高度宽度,但是当第二次我们更改为纵向模式时,它不是更改高度和宽度。 在负载: -
[[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]
[self.view setNeedsDisplay];
在上面的开关案例中使用此代码,此代码用于重新加载视图,以便您获得更新的视图宽度和高度。
如果您只是希望视图具有其超视图的界限并且不使用自动布局,则具有灵活高度和宽度的自动调整蒙版应该只要在超视图的边界发生更改时设置正确的帧。
编辑
您正在将子视图的帧的原点设置为超级视图的原点 - 我怀疑这不是预期的。 如果超级视图从(0,0)移动,则会将子视图的原点移动相对于窗口的两倍移位。
由于界面方向改变而更新UI是一项相当常见的任务,因此有一个很好的支持方法来执行此操作。
解决方案1:
如果您使用的是基于框架的布局(如示例代码中所示),建议您将框架设置代码放入视图的layoutSubviews
如Jan Greve所评论的那样。 要做到这一点,你必须继承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的view属性,它就可以正常工作。
解决方案2:
解决方案1通常可以使用,但在您的特定情况下,可以通过自动调整掩码来完成布局。 (在这里,我必须再次向Jan Greve请求,他在答案中推荐了这个。)现在你不需要子类UIView,可以从viewcontroller处理所有东西。 您可以在viewDidLoad
插入这样的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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.