我想在我的UINavigationBar中有一个titleView,它有两行文本而不是一行
当我有一个"Back"-Button和一个"Edit"按钮时,我当前的实现效果很好,因为它看起来几乎居中.问题是我只有一个按钮.它看起来非常奇怪和错误,因为视图集中在可用空间上.
这是我目前的实施:
CGRect frame = self.navigationController.navigationBar.frame;
UIView *twoLineTitleView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(frame), 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, CGRectGetWidth(frame), 14)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text = self.title;
[twoLineTitleView addSubview:titleLabel];
UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 23, CGRectGetWidth(frame), 14)];
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
subTitleLabel.textAlignment = UITextAlignmentCenter;
subTitleLabel.text = self.subTitle;
[twoLineTitleView addSubview:subTitleLabel];
self.navigationItem.titleView = twoLineTitleView;
Run Code Online (Sandbox Code Playgroud) 我目前使用此代码来设置导航项的titleView:
- (void)viewDidLoad
{ ...
UIImage *navbarTitle = [UIImage imageNamed:@"navbartitleview1"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:navbarTitle];
self.navigationItem.titleView =imageView;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用UIAppearance来设置这段代码?
[UINavigationItem appearance]
Run Code Online (Sandbox Code Playgroud)
无效.
我在iPhone应用程序中有一个导航栏,其中包含自定义UIBarButtonItems:
UIBarButtonItem* homePersonButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"homePerson.png"]
style:UIBarButtonItemStylePlain
target:self action:@selector(showHomePerson)];
Run Code Online (Sandbox Code Playgroud)
homePerson.png是20 x 18.
它在纵向模式下看起来很不错,但在横向模式下,按钮上的20x18图像太高,看起来不太好.看起来iOS在标准按钮上做了一些工作,以便在更薄的导航栏中根据需要缩小图像.
处理这个问题的最佳方法是什么?
另一个例子是我在导航栏的titleView中有一个自定义按钮.我还需要它在应用程序旋转时缩小.
在此先感谢你能给我的任何指示!
iphone uinavigationbar uibarbuttonitem uinavigationitem titleview
我可能在这里做错了,因为这看起来有点愚蠢.
我在我的UINavigationController上设置了一个自定义titleView(以UILabel的形式),在每个页面上都是相同的.为了实现这一点,我在我的app delegate中创建了一个函数来正确显示标签.然后我将其推送到导航堆栈后,在任何子视图上调用此函数.
这是代码(可能比我的解释更有意义):
//In MyAppDelegate.m:
- (void)showTitleForNavigationController:(UINavigationController*) navController {
UILabel *label = [[UILabel alloc] init];
// set up label attributes
// ...
[label sizeToFit]; //without this line my label won't show at all
[navController.navigationBar.topItem setTitleView:label];
[label release];
}
// In SomeViewController.m, when pushing another controller onto the stack:
UIViewController *otherViewController = //initialize other view controller;
[self.navigationController pushViewController:otherViewController animated:YES];
[(MyAppDelegate*)[[UIApplication sharedApplication] delegate] showTitleForNavigationController:otherViewController.navigationController];
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我将下一个视图控制器推入堆栈,并且新控制器平滑地滑动时,在动画的整个持续时间内,标签粘贴在左上方,然后在动画结束后最终捕捉到位.它看起来很奇怪和丑陋.如何正确设置标签,使其从下一个视图平滑滑动?当然,这很简单,我很想念......
我使用一个按钮作为我的标题视图UITableViewController,打开一个类别的下拉列表.选择类别会按所选类别过滤表格视图的内容.
按钮显示所选类别的名称加上一个小箭头,类似于iBooks的外观(或者看起来仍然看起来?我暂时没有使用它).因此,我希望它具有与标准标题相同的行为,并且首先使其变大,并在滚动表视图时折叠.
有没有办法做到这一点?
谢谢
我试图理解它是一个错误或它是预期的行为.
在iOS 10及更早版本中,我们可以使用设置自定义标题navigationItem.titleView.
在iOS的11,我们设置时navigationItem.largeTitleDisplayMode = .always并设置navigationItem.titleView = <Some cool UIButton>它的显示都正常航行标题栏和导航的大标题.
总结一下:
我们如何在大型导航标题上使用自定义titleView?
编辑:这是预期的结果:
我有一个iOS应用程序,我在其中设置自定义导航标题视图.
它在iOS 10之前工作正常,但在iOS 11中,导航标题视图放错了位置.
这是iOS 10的屏幕截图 -
这是iOS 11的屏幕截图 -
正如您在屏幕截图中看到的那样,当我在iOS 10上运行代码时,标题视图看起来很好.但是iOS 11上的相同代码会将标题视图向下移动一些像素并且会被切断.
这是我设置标题视图的方式 -
navigationItem.titleView = MY_CUSTOM_TITLE_VIEW
我尝试了很多东西,并寻找了许多解决方案,但没有任何工作.
我正在使用基于UIImageView的动画来显示自定义活动指示器.以下代码负责设置:
+ (void)startLoadingActivity:(UIViewController *)vc {
vc.parentViewController.navigationItem.titleView = [UIImageView imageViewWithPath:@"loading_" count:10 duration:1.3 frame:CGRectMake(0, 0, 22, 22)];
}
Run Code Online (Sandbox Code Playgroud)
这是UIImageView + Animation类别的要点.
每当我启动Web请求时,我都会启动活动指示器,每当请求完成时,我都会通过将titleView属性设置为来停止它nil.
结果是这样的:

请求方法也被调用到-viewDidAppear:视图控制器的方法中.
但是,每当动画在转换期间开始时,它的位置似乎UIImageView都是错误的.
以下结果显示了问题:
请注意活动指示符如何显示在"组"按钮之上.

这个问题来自哪里?我该如何解决?
我正在尝试在self.navigationItem.rightButton上设置一个按钮来切换放在self.navgivationItem.titleView中的分段控件.但是这将删除创建导航栏时首先由self.title设置的标题..我不知道我的方法是否不好,但我想我可以在标题视图中的UILabel和分段控件之间旋转.
它按照我的意愿工作,但是我无法弄清楚导航栏中的默认标题是什么大小和字体以及shadowoffset和shadowcolor ..你能不能帮我解决一个不强制我覆盖navigationItem.titleView的解决方案或者帮我弄清楚使UILabel外观与默认标题完全相同所需的信息.
感谢任何帮助.
iphone uisegmentedcontrol uilabel uinavigationitem titleview
我想在我的UINavigationBar中在左右BarButtonItem之间使用自定义UIView,但要尽可能宽.出于这个原因,我在IB中添加了一个UIView到NavigationBar.禁用自动布局后,一切都按预期使用自动调整遮罩.但是在启用了autolayout的故事板中,我无法让它工作.看起来我不能在标题视图中为IB设置任何约束.如果我将设备旋转到横向模式,则UIView的宽度仍然相同.我该怎么做才能使titleView填充我的UIBarButtonItems之间的空间并启用autolayout?
感谢您的任何帮助
Linard
cocoa-touch constraints uinavigationbar titleview autolayout
titleview ×10
ios ×6
ios11 ×3
animation ×2
cocoa-touch ×2
iphone ×2
autolayout ×1
constraints ×1
ios10 ×1
objective-c ×1
uiappearance ×1
uiimageview ×1
uilabel ×1