ios 控制 UINavigationItem titleView 的大小/位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16069744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Controlling size / position of UINavigationItem titleView
提问by Ser Pounce
In my app, I have some custom titles (with lettering that isn't a font) stored in pngs that I want to put as the title of my navigation. I want the lettering in the titles all to be the same size for each different view controller, so in illustrator I've worked on making it all the same size and width (affording blank space to account for shorter strings). I do the following:
在我的应用程序中,我有一些自定义标题(带有不是字体的字母)存储在 png 中,我想将它们作为导航的标题。我希望标题中的字母对于每个不同的视图控制器都具有相同的大小,因此在 illustrator 中,我一直致力于使其大小和宽度都相同(提供空白以解释较短的字符串)。我执行以下操作:
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectAnAlbumTitleLettering"]];
titleImageView.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = titleImageView;
[titleImageView release];
And it seems the image is arbitrarily resized and positioned based on the elements included on each navigationBar
(i.e. back button, right button etc.).
并且图像似乎是根据每个图像上包含的元素navigationBar
(即后退按钮、右键等)任意调整大小和定位的。
I'm wondering, how can I gain control of titleView
so I can make it implement the size and position that I want and so it isn't arbitrarily resized / repositioned.
我想知道,我怎样才能获得控制权,titleView
这样我才能让它实现我想要的大小和位置,所以它不会被任意调整大小/重新定位。
回答by Jeff Kelley
Instead of using your image view as the title view, create another view that contains the image view. Set thatview as the title view of the navigation item; now you're free to adjust the image view's frame within the title view.
不要使用图像视图作为标题视图,而是创建另一个包含图像视图的视图。将该视图设置为导航项的标题视图;现在您可以在标题视图中自由调整图像视图的框架。
回答by Nishant Tyagi
You can control size and position of UINavigationbar titleview. Don't set imageview as titleview directly. Instead create a custom UIView and then set its frame as per your requirement and add your titleImageView as its subview.
您可以控制 UINavigationbar 标题视图的大小和位置。不要直接将 imageview 设置为 titleview。而是创建一个自定义 UIView,然后根据您的要求设置其框架并将您的 titleImageView 添加为它的子视图。
UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];// Here you can set View width and height as per your requirement for displaying titleImageView position in navigationbar
[backView setBackgroundColor:[UIColor greenColor]];
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectAnAlbumTitleLettering"]];
titleImageView.frame = CGRectMake(45, 5,titleImageView.frame.size.width , titleImageView.frame.size.height); // Here I am passing origin as (45,5) but can pass them as your requirement.
[backView addSubview:titleImageView];
//titleImageView.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = backView;
Hope it helps you.
希望对你有帮助。
回答by user3109460
In Swift, you can do it like this:
在 Swift 中,你可以这样做:
var titleView = UIView(frame: CGRectMake(0, 0, 100, 40))
var titleImageView = UIImageView(image: UIImage(named: "cocolife"))
titleImageView.frame = CGRectMake(0, 0, titleView.frame.width, titleView.frame.height)
titleView.addSubview(titleImageView)
navigationItem.titleView = titleView
回答by Kakumanu siva krishna
Updated Swift4 code:
更新 Swift4 代码:
var titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
var titleImageView = UIImageView(image: UIImage(named: "headerlogo"))
titleImageView.frame = CGRect(x: 0, y: 0, width: titleView.frame.width, height: titleView.frame.height)
titleView.addSubview(titleImageView)
navigationItem.titleView = titleView