使用UIAppearance协议和setBackgroundConfiguration方法来手动配置背景。例如,以下代码可以设置一个带有标题和背景颜色的UIButton:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem]; [myButton setTitle:@"My Button" forState:UIControlStateNormal]; [myButton setBackgroundColor:[UIColor blueColor]];
// Set background configuration UIBackgroundConfiguration *backgroundConfig = [UIBackgroundConfiguration clearConfiguration]; [myButton setBackgroundConfiguration:backgroundConfig];
// Set font UIFont *buttonFont = [UIFont systemFontOfSize:18]; [[UIButton appearance] setTitleTextAttributes:@{NSFontAttributeName : buttonFont} forState:UIControlStateNormal];
这段代码保持了背景配置并设置了字体。