UIButton图片的contentMode(backgroundImage与image)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18683985/article/details/84851811

首先我们要明确一点.UIButton有"两张"图片.backgroundImage和image.一个用于背景(在控件的上面一层)、一个在最上层和文字是同一层级.

首先contentMode是UIView对象的属性.
然后UIButton的contentMode并不会影响这两个属性.

// return title and image views. will always create them if necessary. always returns nil for system buttons
@property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel NS_AVAILABLE_IOS(3_0);
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0);

只要不是system buttons的buttontype是不会返回nil的.举个例子:system buttons中的add样式的"+号"图片其实是在backgroundImage上的.那么.这个imageView肯定就是setImage forstate有关的imageView了.

所以进行如下设置来改变setImage forState中的image对应控件的contentMode

	UIbutton *button = [[UIButton alloc] init];
	button.imageView.contentMode = XXXMode;

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/84851811