- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 UINavigationBar 并设置了 leftBarButtonItem 和 rightBarButtonItem。左栏设置为:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
UIBarButtonItem *myButton = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
self.navigationItem.leftBarButtonItem = myButton;
右栏按钮设置为:
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *doneButtonImage = nil;
doneButtonImage = [UIImage imageNamed:@"done.png"];
[doneBtn setImage:doneButtonImage forState:UIControlStateNormal];
doneBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];
self.navigationItem.rightBarButtonItem = doneBarButtonItem;
back 的宽度是 23px,done 是 72px,所以它们不相等。现在的问题是我有一个标签,我想一直在中心..如果标签中的文本太长以至于它干扰了右栏按钮,我想剪辑文本。这是我的设置方式:
titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.text = self.pageTitle; // Can't change
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.clipsToBounds = YES;
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont fontWithName:@"PTSans-Narrow" size:30.0];
titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
titleLabel.textAlignment = UITextAlignmentCenter;
[titleLabel sizeToFit];
titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;
self.navigationItem.titleView = titleLabel;
但是这并没有使标题居中。我该怎么做呢?
最佳答案
我建议先算一下self.pageTitle的宽度然后相应地设置 titleLabel 的位置。我更喜欢这个:-
titleLabel = [[UILabel alloc] init];
titleLabel.text = self.pageTitle; // Can't change
**CGSize maximumTitleLabelSize = CGSizeMake(200,40);//Set maximum width that an self.pageTitle can have.
CGSize expectedLabelSize = [titleLabel.text titleLabel.font
constrainedToSize:maximumTitleLabelSize
lineBreakMode:UILineBreakModeWordWrap];
//adjust the label the the new width.
CGRect newFrame = titleLabel.frame;
newFrame.size.width = expectedLabelSize.width;
titleLabel.size.width = newFrame.size.width;**
if(titleLabel.size.width==100)
{
titleLabel.frame = cgRectMake(85,5,titleLabel.size.width,30);
}
else if(titleLabel.size.width==200)
{
titleLabel.frame = cgRectMake(36,5,titleLabel.size.width,30);
}
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.clipsToBounds = YES;
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont fontWithName:@"PTSans-Narrow" size:30.0];
titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
titleLabel.textAlignment = UITextAlignmentCenter;
[titleLabel sizeToFit];
titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;
self.navigationItem.titleView = titleLabel;
试试这个。它会对你有帮助。谢谢 :)
关于iphone - 在 UINavigationBar 中居中 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10222469/
我有一个带有导航栏的 viewController。我想增加导航栏的高度,但我不能。我已经在寻找增加导航栏高度的方法,但没有成功。 是否有任何增加导航栏或任何其他 lib/alternatives 的
是否可以在左侧或右侧放置自定义图像 UIBarButton 项 边缘一个 UINavigationBar?如果按钮的边缘接触 UINavigationBar 的边缘,我创建按钮的方式看起来最好。 最佳
我喜欢将我的 View 创建为独立的 Xib 文件,然后实例化它们并添加为 subview 。 因此,当使用 UINavigationBar 时,我希望能够做同样的事情,首先创建我的自定义 View
我正在创建一个 iOS 7 应用程序,其中我希望在 NavigationBar 下方有一个 SearchBar,我希望它们看起来像一个整体。因此,我需要用相同的颜色(已经完成)为它们着色,并删除 Na
我有一些UIViewControllers,它们都有纯背景颜色。但是,在用户的个人资料上,我想在背景上显示模糊的个人资料图像,因此我需要一个透明的 UINavigationBar。这很好用。我的做法如
更改 UINavigationBar 背景图像并使主 UINavigationBar 缩放到背景图像大小的最简单方法是什么? 我设计了一个导航栏,它不是我的 TableView Controller
我正在创建一个 iOS 7 应用程序,我希望在其中的 NavigationBar 下方有一个 SearchBar,并且我希望它们看起来像一个整体。因此,我需要用相同的颜色(已经完成)给它们着色,并删除
我需要将导航栏设置为自定义颜色,下面的代码将执行此操作: [[UINavigationBar appearance] setBackgroundImage:navigation
我添加了带有背景图片的 UINavigation 栏。 但不幸的是,UINavigation Bar 中间显示了一条不需要的 1px 细线。 我想隐藏那条线,但它不起作用。我添加了以下代码。 self
我有一个简单的应用程序,它是一个 UITableViewController。该应用程序使用自定义主题,将主题应用于背景和 UINavigationBar。从这个角度来看,我有一个 UINavigat
我在我的名为 BaseViewController 的 Superview 中以编程方式添加了 UINavigationBar,它继承了 UIViewController。但是当我尝试为 UINavi
我正在开发一个具有多个嵌套 UIViewController 的应用程序。我有两个 View ,一个是带有 uibarbuttonitem 的 Root View Controller ,连接到另一个
我知道我可以单独更改导航栏的字体,如以下答案所述:Change the navigation bar's font 目前我正在使用更全局的方法: //in my app delegate: [
我正在尝试从 Tinder 重新创建导航行为。为此,我翻译了一个 Framework delivering the NavigationBar进入 Swift 3。 测试应用程序非常简单。我创建了一个
我能否以某种方式使 UINavigationBar 在 iOS 7 和 iOS 8 中看起来像 iOS 6 UINavigationBar(具有蓝色背景颜色等)? 提前致谢。 最佳答案 我很惭愧地承认
在我的iPad应用程序中,我有一个应用程序设置 View 。选项之一允许用户切换界面颜色方案。通过segue将设置 View 加载到与“主”应用程序窗口不同的单独 View Controller 中。
我制作了一个自定义的 UIStoryboardSegue 但当它展开时,它似乎导致我的 UINavigationBar 变黑,然后又恢复到正确的颜色。请参阅下面的 GIF。 我的自定义segue只是让
下面的代码对我不起作用,任何人都可以帮助找出问题所在吗? var image = UIImage(named: "10384605_10152519403846670_5189785375955620
我想更改导航标题的字体和颜色..所以,为此我已经在下面的代码中完成了这个......但它不起作用...... if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@
我对iOS 7有一些奇怪的麻烦。我的应用程序中有一个UINavigationBar,它非常适合iOS 6; - IOS 6 - 但是,当我尝试在iOS 7上运行它时,后退按钮消失了。它在那里,仍在工作
我是一名优秀的程序员,十分优秀!