如何将 UIImageView 添加到导航栏的右侧?
How can I add an UIImageView to the right side of a navigation bar?
我尝试在导航栏的最右侧显示 UIImageView
。
我尝试在导航栏中添加一个 UIViewcontrol,尽可能地调整它的大小,然后添加一个 UIImageView
并在其后添加一个 [=33] =] 的 0.
在情节提要中它看起来不错,但是当我 运行 我的 iPhone 上的应用程序时,图像和右侧之间有一个巨大的 space。
有什么方法可以删除这个 space?
编辑:
如您所见,iPhone 右侧有一个巨大的 space,它不在故事板中。因为我试图只用故事板来做,所以没有我可以展示的代码。
使用方法 initWithImage:style:target:action: 或 initWithCustomView: 创建一个 UIImageBarButton 并将其添加到导航项作为右栏按钮项:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"imageName"] style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.rightBarButtonItem = myBarButtonItem;
}
创建自定义视图并将其分配给 navigationItem.titleView
- (void)viewDidLoad {
[super viewDidLoad];
UIView *temp = [[UIView alloc] initWithFrame:...];
UIImage *image = [[UIImage alloc] initWithFrame:...];
[temp addSubview:image];
self.navigationItem.titleView = temp;
}
将下面的代码用于带有图像的右键:
UIButton *btn = [UIButton alloc] initWithFrame:...];
[btn setImage:[UIImage imageNamed:@"..."] forState:UIControlStateNormal];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = rightButton;
我尝试在导航栏的最右侧显示 UIImageView
。
我尝试在导航栏中添加一个 UIViewcontrol,尽可能地调整它的大小,然后添加一个 UIImageView
并在其后添加一个 [=33] =] 的 0.
在情节提要中它看起来不错,但是当我 运行 我的 iPhone 上的应用程序时,图像和右侧之间有一个巨大的 space。
有什么方法可以删除这个 space?
编辑:
如您所见,iPhone 右侧有一个巨大的 space,它不在故事板中。因为我试图只用故事板来做,所以没有我可以展示的代码。
使用方法 initWithImage:style:target:action: 或 initWithCustomView: 创建一个 UIImageBarButton 并将其添加到导航项作为右栏按钮项:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"imageName"] style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.rightBarButtonItem = myBarButtonItem;
}
创建自定义视图并将其分配给 navigationItem.titleView
- (void)viewDidLoad {
[super viewDidLoad];
UIView *temp = [[UIView alloc] initWithFrame:...];
UIImage *image = [[UIImage alloc] initWithFrame:...];
[temp addSubview:image];
self.navigationItem.titleView = temp;
}
将下面的代码用于带有图像的右键:
UIButton *btn = [UIButton alloc] initWithFrame:...];
[btn setImage:[UIImage imageNamed:@"..."] forState:UIControlStateNormal];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = rightButton;