5

我的客户端无法读取 iPhone 的默认字体,尺寸太小。我有一个带有导航栏的应用程序,我需要将其中的所有内容都放大,例如字体大小。

IB似乎不允许这样做……有什么帮助吗?

非常感谢!

4

6 回答 6

13

更新:今天(2012 年)自定义 UI 的趋势要大得多,所以我想说下面的答案太苛刻了。但是,仍然没有支持自定义高度的方法,但是您当然可以从 UINavigationBar 派生并覆盖一些调整大小的方法。这可能不会让你被拒绝(尽管它仍然是一个灰色区域,只是苹果今天可能会忽略的东西)。

获得所需大小后,您可以使用 iOS 5 自定义 API 添加自定义背景图像(请参阅 WWDC 2011 Session 114 - Customizing the Appearance of UIKit Controls)。

2009年的原始答案:

这通常是不可能的。

更重要的是,我认为让导航栏更高是违反 Apple Human Interface Guidelines 的,你的应用程序可能会因此而被 App Store 拒绝。请确保您的客户在继续之前了解此风险。

(指出拒绝风险通常是说服客户不要做出无意义决定的好方法。)

于 2009-05-21T14:11:18.987 回答
11

这里的许多答案都不正确或不完整,所以我想在这里添加我的答案,希望它可以启发一些人。

首先,改变导航栏的高度并没有错。评论说它不允许或违反准则的人只是误解了这些准则。

自 iOS 5 以来,调整或更改 UINavigationController 内使用的默认导航栏的功能已成为 SDK 的一部分。

- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass NS_AVAILABLE_IOS(5_0);

更改状态栏高度的最简单方法是在初始化导航控制器时使用此方法,传入您的自定义 UINavigationBar 子类。

TestViewController *t = [[TestViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];
[nav setViewControllers:@[t]];  
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];

这种自定义 UINavigationBar 类的示例可能如下所示:

@interface MyNavigationBar : UINavigationBar
@end

@implementation MyNavigationBar

- (CGSize)sizeThatFits:(CGSize)size
{
        CGSize s = [super sizeThatFits:size];
        s.height = 90; // Or some other height
        return s;
}

@end
于 2014-12-15T12:03:09.467 回答
7

如果您决定只更改导航栏中的字体大小,您可以这样做(通常在您UIViewControllerviewDidLoad方法中):

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];

[titleLabel setBackgroundColor:[UIColor clearColor]];
// here's where you can customize the font size
[titleLabel setFont:[UIFont boldSystemFontOfSize:18.0]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:self.title];
[titleLabel sizeToFit];
[titleLabel setCenter:[self.navigationItem.titleView center]];

[self.navigationItem setTitleView:titleLabel];

[titleLabel release];
于 2009-07-21T20:01:47.020 回答
5

通过子类化,您可以实现并且仍然支持 iOS 3+

完整示例:

#import <UIKit/UIKit.h>

@interface ASNavigationBar : UINavigationBar
@property (nonatomic , retain) UIImage *backgroundImage;
@end

和实施:

#import "ASNavigationBar.h"

@implementation ASNavigationBar
@synthesize backgroundImage = _backgroundImage;

-(void) setBackgroundImage:(UIImage *)backgroundImage
{
    if (_backgroundImage != backgroundImage)
    {
        [_backgroundImage release];
        _backgroundImage = [backgroundImage retain];
        [self setNeedsDisplay];
    }
}

-(void) drawRect:(CGRect)rect
{
    // This is how the custom BG image is actually drawn
    [self.backgroundImage drawInRect:rect];
}

- (CGSize)sizeThatFits:(CGSize)size 
{
    // This is how you set the custom size of your UINavigationBar
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGSize newSize = CGSizeMake(frame.size.width , self.backgroundImage.size.height);
    return newSize;
}
@end

重要笔记:

  1. 如果背景图片带有透明区域,则必须将其 barStyle 属性设置为“半透明”,否则透明区域将为黑色。
  2. 如果 NavigationBar 的高度超过 44 磅,则必须考虑 BarButtonItems 的位置可能不正确。它们都将固定在栏的底部。您可以通过覆盖 layoutSubviews 并更改它们的 origin.y 值来解决此问题。
于 2012-03-21T07:55:44.543 回答
1

您不应更改导航栏的高度。来自 Apple Programming Guide on View Controller:

自定义导航栏外观

在导航界面中,导航控制器拥有它的 UINavigationBar 对象并负责管理它。不允许更改导航栏对象或直接修改其边界、框架或 alpha 值。但是,允许修改一些属性,包括:

● barStyle 属性

● 透光性

● tintColor 属性

(取自苹果:https ://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html )

更新——IOS 7——仍然只能更改可用的属性,但下面是关于如何在导航栏中实现灵活性的精彩教程http://www.appcoda.com/customize-navigation-status-bar-ios -7/

于 2013-12-16T07:29:33.573 回答
0

要添加到 Skela 的答案:

如果您在情节提要中启动导航控制器,则可以将情节提要中 UINavigationBar 的类更改为自定义导航栏。

在此处输入图像描述

在此处输入图像描述

然后在类中实现改变高度

@interface MyNavigationBar : UINavigationBar
@end

@implementation SwitchAssessmentNavigationBar

- (CGSize)sizeThatFits:(CGSize)size
{
        CGSize s = [super sizeThatFits:size];
        s.height = 200; // Or some other height
        return s;
}

@end
于 2016-05-17T09:39:17.587 回答