Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 导航栏标题对齐问题_Ios_Objective C_Ios7_Uinavigationbar - Fatal编程技术网

Ios 导航栏标题对齐问题

Ios 导航栏标题对齐问题,ios,objective-c,ios7,uinavigationbar,Ios,Objective C,Ios7,Uinavigationbar,我已经将自定义视图设置为导航栏标题视图,当页面是第一个视图时,控制器标题将正确显示在中间 但当视图控制器从另一个视图控制器推送时,标题将向右移动 代码: -(void)setUpTwoLineNavigationTitle { CGFloat width = 0.95 * self.view.frame.size.width; UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width,

我已经将自定义视图设置为导航栏标题视图,当页面是第一个视图时,控制器标题将正确显示在中间

但当视图控制器从另一个视图控制器推送时,标题将向右移动

代码:

-(void)setUpTwoLineNavigationTitle {
    CGFloat width = 0.95 * self.view.frame.size.width;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
    contentView.backgroundColor = [UIColor clearColor];

    CGRect titleRect = contentView.frame;
    titleRect.origin.y = 4;
    titleRect.size.height = 20;

    UILabel *titleView = [[UILabel alloc] initWithFrame:titleRect];
    titleView.backgroundColor = [UIColor clearColor];
    titleView.font = [UIFont systemFontOfSize:14.0];
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.textColor = [UIColor blackColor];
    titleView.text = @"";
    [contentView addSubview:titleView];

    CGRect subTitleRect = contentView.frame;
    subTitleRect.origin.y = 24;
    subTitleRect.size.height = subTitleRect.size.height - 24;
    //CGRect subtitleFrame = CGRectMake(0, 24, 220, 44-24);
    UILabel *subtitleView = [[UILabel alloc] initWithFrame:subTitleRect];
    subtitleView.backgroundColor = [UIColor clearColor];
    subtitleView.font = [UIFont systemFontOfSize:11.0];
    subtitleView.textAlignment = NSTextAlignmentCenter;
    subtitleView.textColor = [UIColor blackColor];
    subtitleView.text = @"";
    [contentView addSubview:subtitleView];

    contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    self.navigationItem.titleView = contentView;
}
有人能帮我纠正这个吗


Github链接:

这里有一个解决方法,它可能很有用,其思想是使用UILabel作为
导航项的
标题视图。这样,您不必手动计算和调整其框架,它将自动由系统居中。但代码中的范围是硬编码的

// here to create a UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;

// set different font for title and subtitle
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Title\nSubTitle"];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:NSMakeRange(0,5)];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11.0] range:NSMakeRange(6,8)];

// set line spacing
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineSpacing:6];
[paragrahStyle setAlignment:NSTextAlignmentCenter];
[string addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [string length])];

label.attributedText = string;
[label sizeToFit];

self.navigationItem.titleView = label;

您的contentView太宽,因此通过“后退”按钮将其向右推。是否需要将其设置为视图宽度的0.95倍?如果是宽度的0.6倍,它看起来会居中。@rdelmar谢谢,但如果我这样做,那么在加载子视图时,最初它看起来是左对齐的,然后移到了中心。我在测试应用程序中看不到这种情况。在任何情况下,都需要缩小视图以使其居中。为了解决你提到的问题,你可能还需要做其他的事情。请看我的回答:工作很有魅力,谢谢