objective-c - 插入 UIImageView 作为导航栏按钮

标签 objective-c button navigation uiimageview

我是 Xcode 和 Objective-C 的新手,我尝试制作一个应用程序,其导航菜单可以显示保存在 MySQL 数据库中的所有者的个人资料图片。我使用了导航栏按钮,但似乎无法使用 UIViewImage 作为按钮。这是我的代码:

//Resize image
   UIImage *miniProfile=[UIImage imageNamed:@"profile_icon.png"];//this is the hardcoded version
   UIImage *tempImage = nil;
   CGSize targetSize = CGSizeMake(25,25);
   UIGraphicsBeginImageContext(targetSize);
   CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
   thumbnailRect.origin = CGPointMake(0.0,0.0);
   thumbnailRect.size.width  = targetSize.width;
   thumbnailRect.size.height = targetSize.height;
   [miniProfile drawInRect:thumbnailRect];
   tempImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   //Automatically change image to circle view
   UIImageView *roundProfile = [[UIImageView alloc] initWithImage:tempImage];
   roundProfile.backgroundColor = [UIColor clearColor];
   roundProfile.layer.cornerRadius = miniProfile.size.width/2;
   roundProfile.layer.masksToBounds = YES;
   [self.view addSubview: roundProfile];

   //Add image to navigation bar. However button cannot be clicked
   UIBarButtonItem * profileImage = [[UIBarButtonItem alloc] initWithCustomView:roundProfile];

   //Create two button on right navigation bar. One for image the other for real button
   NSArray *profilebarButton = @[profileImage];
   self.navigationItem.rightBarButtonItems = profilebarButton;

我可以将 UIImageView 放在导航栏中,但有两个问题: 1.) 如果图像大于 30x30,则图像不会出现,意味着调整大小不起作用。 2.) 我无法单击图像,需要创建另一个按钮来处理此问题

最佳答案

您可以使用此代码来自定义导航栏按钮

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];

//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"btn_next_arrow_01.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"btn_next_arrow_02.png"] forState:UIControlStateHighlighted];
button.adjustsImageWhenDisabled = NO;


//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);

[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
self.navigationItem.hidesBackButton = YES;

关于objective-c - 插入 UIImageView 作为导航栏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377253/

相关文章:

ios - 为什么 UILongPressGestureRecognizer 在 iOS 模拟器中工作但不适用于 iPhone

html - 为什么这两个按钮渲染不同

android - Jetpack Compose 中的导航。按钮点击问题

ios - UIButton 被点击两次

objective-c - 永远不会调用 NSFilePresenter 方法

ios - 一起使用 Firebase 和 Google 分析?

iphone - 使用 iPhone 相机

javascript - 添加一个 Javascript 按钮来更改 iframe 的内容

android - 导航架构 fragment 重新加载问题

java - 如何操作按钮在 Java 中为 GUI 打开新 Pane