Akh*_*iji 5 objective-c uiview ios landscape-portrait autolayout
我有一个UIView以编程方式创建的子类。
我的应用程序中有纵向和横向模式,使用自动布局。
最初UIView使用 设定框架initWithFrame,但当方向更改为横向时, 的CGRect则UIView停留在纵向模式。
如何提醒子UIView类更改CGRect设备方向?
这是我的初始框架设置代码UIView:
- (id)initWithFrame:(CGRect)frame videoUrl:(NSURL *)videoUrl{
self = [super initWithFrame:frame];
if (self) {
_frame_width = frame.size.width;
int thumbWidth = ceil(frame.size.width*0.05);
_bgView = [[UIControl alloc] initWithFrame:CGRectMake(thumbWidth-BG_VIEW_BORDERS_SIZE, 0, frame.size.width-(thumbWidth*2)+BG_VIEW_BORDERS_SIZE*2, frame.size.height)];
_bgView.layer.borderColor = [UIColor grayColor].CGColor;
_bgView.layer.borderWidth = BG_VIEW_BORDERS_SIZE;
[self addSubview:_bgView];
}
}
Run Code Online (Sandbox Code Playgroud)
只需检测设备的方向是否处于landscape或portrait模式,然后更改大小UIView请参见下面的代码:
要检测设备方向:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
UIView *view = [classObject setupView:xPos yPos:yPos width:widthValue height:heightValue];
yourView.frame = view.frame;
NSLog(@"device orientation Landscapse");
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
UIView *view = [classObject setupView:xPos yPos:yPos width:widthValue height:heightValue];
yourView.frame = view.frame;
NSLog(@"device orientation portrait");
}
}
Run Code Online (Sandbox Code Playgroud)
在您的类中编写此方法,无需从任何地方调用,它会在用户旋转时检测方向。
在类中创建自定义方法,该方法返回UIView完整的框架,并在用户更改方向时通过发送 x,y 位置及其宽度和高度进行调用,请参阅下面的代码:
-(UIView *) setupView:(float)x yPos:(float)y width:(float)width height:(float) height{
UIView *yourView = [[UIView alloc] initWithFrame:CGRectMake(x,y,width,height)];
yourView.backgroundColor=[UIColor clearColor];
return yourView;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
859 次 |
| 最近记录: |