我试图绕过愚蠢的苹果‘没有自定义字体与自动布局’的错误,但我遇到了问题,实现它的一种方式,也将工作特点收集更新。
我最初是在viewDidLoad上的延迟调用函数的0.5个延迟时间上运行代码的,在那里,它正确地更新了设备的字体大小和字体类型。但是,我了解到traitCollectionDidChange在这段时间内也被调用,所以我将代码移到那里。现在,字体类型没有更新,但字体大小正在更新。
这是我的代码,这可能是我忽略的愚蠢之处:
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
//[super traitCollectionDidChange: previousTraitCollection];
//Create horz/vert vars for easier iffing
UIUserInterfaceSizeClass viewHorizontal = self.view.traitCollection.horizontalSizeClass;
UIUserInterfaceSizeClass viewVertical = self.view.traitCollection.verticalSizeClass;
//Change font and font size depending on classes; really just the size but we need to set the font anyway
if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassCompact){
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:36];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
} else if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassRegular){
agTtl_Text.editable = YES; //still need this iOS 6 hack?
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:35];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
agTtl_Text.editable = NO; //still need this iOS 6 hack?
}
}发布于 2016-06-04 22:10:22
我终于想明白了。当转换的动画完成后,我必须找到一种更新的方法,并且我找到了通过传递给UIViewControllerTransitionCoordinator的willTransitionToTraitCollection来进行更新的方法。我还必须在viewDidLoad中调用我的字体方法,因为willTransitionToTraitCollection只在设置转换时调用,这不会在应用程序加载时运行,也不会在IPad上运行。
下面是我的代码:
- (void)viewDidLoad
{
[self performSelector:@selector(setFontSettings) withObject:nil afterDelay:0.5];
rtnMsg = [[NSMutableArray alloc] init];
db = [[Database alloc] init];
rtnMsg = [db bDoesDBExist];
if ([rtnMsg[0] boolValue] == NO){
[actBtn setTitle:@"Tap Here to Begin" forState:UIControlStateNormal];
bDBExists = NO;
} else{
//status.text = @"Found database";
[actBtn setTitle:@"Tap Here to Login" forState:UIControlStateNormal];
nAccBtn.hidden = NO;
nAccBtn.enabled = YES;
bDBExists = YES;
//self.navigationItem.title=@"New Patient Profile Creator";
}
[super viewDidLoad];
}
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self setFontSettings];
}];
}
- (void)setFontSettings{
//[super traitCollectionDidChange: previousTraitCollection];
//Create horz/vert vars for easier iffing
UIUserInterfaceSizeClass viewHorizontal = self.view.traitCollection.horizontalSizeClass;
UIUserInterfaceSizeClass viewVertical = self.view.traitCollection.verticalSizeClass;
//Change font and font size depending on classes; really just the size but we need to set the font anyway
if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassCompact){
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:36];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
} else if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassRegular){
agTtl_Text.editable = YES;
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:35];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
agTtl_Text.editable = NO;
} else if ((viewHorizontal == UIUserInterfaceSizeClassRegular && viewVertical == UIUserInterfaceSizeClassRegular) ||
(viewHorizontal == UIUserInterfaceSizeClassRegular && viewVertical == UIUserInterfaceSizeClassCompact)){
agTtl_Text.editable = YES;
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:50];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:34];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
agTtl_Text.editable = NO;
}
}https://stackoverflow.com/questions/37628470
复制相似问题