在 rightbarbuttonitem 中使用方形图像?

发布于 2024-11-10 09:15:58 字数 127 浏览 0 评论 0原文

我想要方形图像周围有常规按钮形状,所以我选择了 initWithImage:Style:Target:Action。问题是,它然后拉伸我的图像以适应宽按钮,而这根本不是我想要的。我如何让它把我的图像放在那里而不拉伸?我该如何改变矩形的宽度?

I want the regular button shape around my square image, so I went for initWithImage:Style:Target:Action. The problem is, it then stretches my image to fit a wide button, and this isn't what i want at all. How do i make it put my image there without stretching? And how would i change the rectangle width too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下伊人醉 2024-11-17 09:15:58

我不确定您是否可以将图像添加到栏按钮上,以便它看起来有轮廓。我确实知道您可以相当轻松地添加自己的自定义视图,而不会拉伸图像。基本上,您所要做的就是创建一个用图像初始化的 imageView,然后创建一个栏按钮项并向其中添加自定义视图。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];  //or whatever image type you have
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStyleBordered target:self action:@selector(handlePress:)];
myButton.customView = imageView;
// now you have your image (unstretched) in place of your bar button item

然后您只需将按钮设置在您想要的位置即可。同样,这不会为您提供按钮背景,因此最好创建一个带有按钮背景的图像(可能将现有图像Photoshop到按钮上,然后保存新图像并使用该图像)

I'm not sure if you can add an image onto a bar button so it looks outlined. I do know that you can add you own custom view fairly easily that doesn't stretch the image. Basically, all you have to do is create an imageView initialized with your image, then create a bar button item and add the custom view to it.

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];  //or whatever image type you have
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStyleBordered target:self action:@selector(handlePress:)];
myButton.customView = imageView;
// now you have your image (unstretched) in place of your bar button item

Then you just have to set the button where you want it. Again, this won't give you a button background, so it might be best to create an image with a button background (possibly photoshop your existing image on a button, then save that new image and use that one)

无名指的心愿 2024-11-17 09:15:58

您可以尝试调整 UIBarButtonItem 的宽度。

rightBarButton.width = 20.0;

You might try adjusting the width of the UIBarButtonItem.

rightBarButton.width = 20.0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文