zoukankan      html  css  js  c++  java
  • Autolayout 下面的 Layer.cornerRadius

    一、问题:

      如何在Autolayout模式中设置一个UIView的layer.cornerRadius?

    二、解决:

      UiView的layer目前还不支持Autolayout设置约束,因此如果想设置一个layer.cornerRadius的大小,必须传递的是一个值。

      如果被设置的UIView尺寸会发生变化,也就是动态的大小,那么就需要在约束发生变化的时候,主动更新layer.cornerRadius对应的大小

      那么具体分两种情况来对待:

      第一种,UIView的尺寸不变化的时候:

      1)在Xcode中设置layer.cornerRadius

      

      2)使用代码

      注意代码的位置,需要在View初始化的时候,比如ViewDidload,initView的时候

    @interface ViewController ()
    
    @property (weak, nonatomic) IBOutlet UIImageView *imgView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor yellowColor];
        self.imgView.backgroundColor = [UIColor blueColor];
    
        self.imgView.layer.cornerRadius = self.view.bounds.size.height/2;
        self.imgView.layer.masksToBounds = YES;
    
    
    }
    @end

      但是需要注意一种情况,如果这个View的尺寸未计算得到的时候,需要放在 viewDidLayoutSubviews (VC),和viewDidLayout中(View)

      第二种,UIView的尺寸变化的时候:

      这种时候因为UIView的尺寸发生变化,那么需要在尺寸信息生成之后,进行调整。

      如果是VC,那么在viewDidLayoutSubviews或者ViewDidAppear的回调中。

      如果是UIView,苹果建议将约束代码放在updateConstraints回调中,这样可以提升性能。

      

    //重写updateViewConstraints方法,进行约束的更新
    - (void)updateViewConstraints {
        [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.view);
            
            // 初始宽、高为100,优先级最低
            make.width.height.mas_equalTo(100 * self.scacle).priorityLow();
            // 最大放大到整个view
            make.width.height.lessThanOrEqualTo(self.view);
        }];
        [super updateViewConstraints];
    }
    
    
    // 通知需要更新约束,但是不立即执行
    [self setNeedsUpdateConstraints];
    // 立即更新约束,以执行动态变换
    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];
    // 执行动画效果, 设置动画时间
    [UIView animateWithDuration:0.2 animations:^{
       [self layoutIfNeeded];
    }];
    

      

      

  • 相关阅读:
    调用Type.InvokeMember()时出现MissingMethodException
    C#学习之Delegate
    WCF之元数据交换 (Metadata Exchange)
    定义Enum的开始和结束,这样就能循环Enum了
    定制自己的Visual Studio的Debugger Visualizer
    C#中 #if DEBUG 和 Conditional("DEBUG")的区别
    从哪里开始学习Windows 8?(zz)
    Macro 小总结
    WPF应用的图标
    如何把 Visutal studio中的“printonbreakpoint”消息打印在程序的任何地方
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/9580399.html
Copyright © 2011-2022 走看看