UIView的UIViewContentMode的效果

2016-09-30  本文已影响24人  Cooperluffy丨路飞

先看一下apple的官方定义:

typedef NS_ENUM(NSInteger, UIViewContentMode) {
    UIViewContentModeScaleToFill,
    UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
    UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
    UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,
};

实验代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor yellowColor];
    bgView.bounds = CGRectMake(0,0,250, 400);
    bgView.center = self.view.center;
    bgView.clipsToBounds = YES;
    [self.view addSubview:bgView];
    
    UIImageView *imv = [UIImageView new];
    imv.backgroundColor = [UIColor blueColor];
    imv.image = [UIImage imageNamed:@"test"];
    imv.frame = CGRectMake(0, 0, 200, 300);
    imv.contentMode  = UIViewContentModeScaleToFill;
    [bgView addSubview:imv];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Paste_Image.png Paste_Image.png Paste_Image.png

配合 imv.layer.masksToBounds = YES;属性使用,多余部分被裁剪掉了

Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读