gpt4 book ai didi

iPhone 导航栏中的标题和副标题

转载 作者:行者123 更新时间:2023-12-03 18:17:21 24 4
gpt4 key购买 nike

在我的应用程序中,我希望导航栏显示标题和副标题。

为此,我将以下代码添加到我的 View Controller 中:

// Replace titleView
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease];
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = YES;

CGRect titleFrame = CGRectMake(0, 2, 200, 24);
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20];
titleView.textAlignment = UITextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = @"";
titleView.adjustsFontSizeToFitWidth = YES;
[_headerTitleSubtitleView addSubview:titleView];

CGRect subtitleFrame = CGRectMake(0, 24, 200, 44-24);
UILabel *subtitleView = [[[UILabel alloc] initWithFrame:subtitleFrame] autorelease];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.font = [UIFont boldSystemFontOfSize:13];
subtitleView.textAlignment = UITextAlignmentCenter;
subtitleView.textColor = [UIColor whiteColor];
subtitleView.shadowColor = [UIColor darkGrayColor];
subtitleView.shadowOffset = CGSizeMake(0, -1);
subtitleView.text = @"";
subtitleView.adjustsFontSizeToFitWidth = YES;
[_headerTitleSubtitleView addSubview:subtitleView];

_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);

self.navigationItem.titleView = _headerTitleSubtitleView;

并且还实现了一个方法:

-(void) setHeaderTitle:(NSString*)headerTitle andSubtitle:(NSString*)headerSubtitle {
assert(self.navigationItem.titleView != nil);
UIView* headerTitleSubtitleView = self.navigationItem.titleView;
UILabel* titleView = [headerTitleSubtitleView.subviews objectAtIndex:0];
UILabel* subtitleView = [headerTitleSubtitleView.subviews objectAtIndex:1];
assert((titleView != nil) && (subtitleView != nil) && ([titleView isKindOfClass:[UILabel class]]) && ([subtitleView isKindOfClass:[UILabel class]]));
titleView.text = headerTitle;
subtitleView.text = headerSubtitle;
}

一切都很顺利,谢谢。

只不过当 iPhone 旋转到横向时,标题+副标题不会像导航项的默认标题那样自动缩小。

有什么指点吗?

谢谢!

最佳答案

titleViewsubtitleView 标签放入 View Controller 的属性中,以便您可以从任何方法访问它们。然后,在旋转 View Controller 时,调整标签的大小:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustLabelsForOrientation:toInterfaceOrientation];
}

- (void) adjustLabelsForOrientation:(UIInterfaceOrientation)orientation {
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
titleView.font = [UIFont boldSystemFontOfSize:16];
subtitleView.font = [UIFont boldSystemFontOfSize:11];
}
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
titleView.font = [UIFont boldSystemFontOfSize:20];
subtitleView.font = [UIFont boldSystemFontOfSize:13];
}
}

关于iPhone 导航栏中的标题和副标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2817181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com