【问题标题】:How to change the frame of a UIView subclass on device orientation which is programmatically created?如何在以编程方式创建的设备方向上更改 UIView 子类的框架?
【发布时间】:2017-02-15 04:43:24
【问题描述】:

我有一个以编程方式创建的UIView 子类。

我的应用程序中有纵向和横向模式,它使用自动布局。

最初UIView 框架是使用initWithFrame 设置的,但是当方向更改为横向时,UIViewCGRect 会停留在纵向模式。

如何提醒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];
    }
}

【问题讨论】:

  • 当app改变方向时,你需要改变view的frame size,对吧?
  • @vaibhav 是的。我必须更改 UIView CGRect。
  • 检查我的ans是否适合..

标签: ios objective-c uiview autolayout landscape-portrait


【解决方案1】:

只需检测设备的方向是否处于landscapeportrait 模式,然后相应地更改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");
    }
}

在你的类中编写这个方法,不需要从任何地方调用它会在用户旋转时检测方向。

在您的类中创建自定义方法,该方法返回带有完整帧的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;
}

【讨论】:

  • 我将此 UIView 子类作为子视图添加到我的视图控制器中,在其中检测方向更改。问题是我必须向 UIView 子类发送触发器以更改方向更改时的帧大小。最初 UIView 框架仅设置为纵向,但当涉及到横向时,框架不会改变。我需要一种方法来在设备方向上向 UIView 的委托方法发送一些触发器。
  • 您是否创建了任何方法来设置和创建类中的视图?你能显示那个类代码吗..
  • " [self.view addSubview:_videoRangeSlider];"我是这样添加的。我有一个单独的 UIView 子类,其中 CGRect 必须更改,还有一个单独的 ViewController,其中 UIView 作为子视图添加。我需要触发从 ViewController 到 UIView 子类的方向。
  • @AkhilJiji 上面我已经发布了一个例子,只要看看,如果还有任何疑问,请告诉我..
【解决方案2】:

您可以将autoresizingMask 用于您的视图和背景视图,以支持方向更改。这些掩码将自动转换为自动布局约束,因为默认情况下启用了translatesAutoresizingMaskIntoConstraints,例如:

@interface MyView : UIView

@property (nonatomic) UIControl* bgView;

@end

@implementation MyView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.redColor;
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        int thumbWidth = ceil(frame.size.width*0.05);
        self.bgView = [[UIControl alloc] initWithFrame:CGRectMake(thumbWidth-BG_VIEW_BORDERS_SIZE, 0, frame.size.width-(thumbWidth*2)+BG_VIEW_BORDERS_SIZE*2, frame.size.height)];
        self.bgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        self.bgView.layer.borderColor = [UIColor grayColor].CGColor;
        self.bgView.layer.borderWidth = BG_VIEW_BORDERS_SIZE;
        [self addSubview:_bgView];
    }
    return self;
}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2012-09-20
    相关资源
    最近更新 更多