Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 控制UINavigationItem标题视图的大小/位置_Iphone_Ios_Cocoa Touch_Uinavigationcontroller_Uinavigationbar - Fatal编程技术网

Iphone 控制UINavigationItem标题视图的大小/位置

Iphone 控制UINavigationItem标题视图的大小/位置,iphone,ios,cocoa-touch,uinavigationcontroller,uinavigationbar,Iphone,Ios,Cocoa Touch,Uinavigationcontroller,Uinavigationbar,在我的应用程序中,我有一些自定义标题(字母不是字体)存储在PNG中,我想将其作为导航的标题。我希望标题中的字母对于每个不同的视图控制器都具有相同的大小,因此在illustrator中,我一直致力于使其具有相同的大小和宽度(为较短的字符串提供空白)。我做了以下工作: UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectAnAlbumTitleLettering"]]

在我的应用程序中,我有一些自定义标题(字母不是字体)存储在PNG中,我想将其作为导航的标题。我希望标题中的字母对于每个不同的视图控制器都具有相同的大小,因此在illustrator中,我一直致力于使其具有相同的大小和宽度(为较短的字符串提供空白)。我做了以下工作:

UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectAnAlbumTitleLettering"]];
titleImageView.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = titleImageView;
[titleImageView release];
而且图像似乎是根据每个
导航栏上包含的元素(即后退按钮、右按钮等)任意调整大小和位置的


我想知道,我怎样才能控制
标题视图
,这样我就可以使它实现我想要的大小和位置,这样它就不会被任意调整大小/位置

创建另一个包含图像视图的视图,而不是将图像视图用作标题视图。将该视图设置为导航项的标题视图;现在,您可以在标题视图中自由调整图像视图的框架。

您可以控制UINavigationbar标题视图的大小和位置。不要直接将imageview设置为titleview。而是创建一个自定义UIView,然后根据需要设置其框架,并将标题图像视图添加为其子视图

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;

希望它能帮助您。

在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
更新的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

关于“为什么”不能直接设置imageView,有什么解释吗?导航标题视图是否使用它做了一些奇怪的事情?@rckehoe系统正在更改标题视图的框架,但它没有更改标题视图子视图的框架。请记住,我们没有实现UINavigationItem。因此,可能会有重新调整尺寸和位置的检查。由于某些原因,这在iOS13.3中不起作用-尤其是当标题视图的高度应。大于40像素(例如大标题导航栏中的150像素)。您将如何增加到更高的高度??您仍然需要确保容器视图具有适当的大小以包含图像视图,因为某些原因,这在iOS13.3中不起作用-尤其是如果标题视图的高度不正确。大于40像素(例如大标题导航栏中的150像素)。您将如何增加到更高的高度??由于某些原因,这在iOS13.3中不起作用-特别是如果标题视图的高度应为。大于40像素(例如大标题导航栏中的150像素)。你将如何提高到更高的水平??