ios - 删除 uinavigationbar 标题边距

标签 ios uinavigationcontroller

我在 app delegate 中修改了我的导航栏,如下所示:

 NSDictionary *settings = @{

                           UITextAttributeFont                 :  [UIFont fontWithName:@"impact" size:36.0],
                           UITextAttributeTextColor            :  [UIColor whiteColor],
                           UITextAttributeTextShadowColor      :  [UIColor clearColor],
                           UITextAttributeTextShadowOffset     :  [NSValue valueWithUIOffset:UIOffsetZero]};

[[UINavigationBar appearance] setTitleTextAttributes:settings];

但是对于较大的字体,字体会像这样缩小。 :

enter image description here

我尝试在我的 VC 的 viewWillAppear 中这样做:

 UIView *newTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[newTitleView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar addSubview:newTitleView];

然后我打算将标题居中对齐。但这似乎不对。我真正需要的只是删除标题标签顶部和底部的边距。我该怎么做。

最佳答案

试试这段代码,不要设置 setTitleTextAttributes

UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];

        titleView.textColor = [UIColor yellowColor]; // Change to desired color

       self.navigationController.navigationItem.titleView = titleView;
        [titleView release];
    }
    titleView.text = title;
    [titleView sizeToFit];

关于ios - 删除 uinavigationbar 标题边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16536379/

相关文章:

ios - Material 设计 snackbar 在 swift 4 中不起作用

iphone - 使用 AVFoundation 在自定义播放器中播放 youtube 视频

ios - 在导航 Controller 中转到 "back"时,ARC 未释放内存 - 它正在减慢我的 uipageviewcontroller

iOS 7 UISearchDisplaycontroller 隐藏导航 Controller 栏

ios - 如何禁用工具栏项?

ios - UITextField 验证

ios - 如何处理 Objective-C 模型关系中的循环引用

ios - OCMock3 与 IOS 的集成问题

ios - 使用 UINavigationController 边缘滑动时 CABasicAnimation 重置

iOS 10 Beta 使导航栏按钮和标题在 pushViewController 上消失