【问题标题】:How to place profile image on uibarbuttonitem with rounded corners in objective c?如何将配置文件图像放置在目标 c 中圆角的 uibarbuttonitem 上?
【发布时间】:2015-07-08 06:32:57
【问题描述】:

我想让我的 UIbarbuttonItem 带有圆角(右侧)。 I referred the link to implement 还是我没有得到。图片来自 URL。我的代码是,

 dispatch_async(dispatch_get_global_queue(0,0), ^{

 NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL   URLWithString:userImage]];
    if ( data == nil )
        return;
    dispatch_async(dispatch_get_main_queue(), ^{
        // WARNING: is the cell still using the same data by this point??
        UIImage *img = [UIImage imageWithData: data];

        UIImage *btnImage = [self imageWithImage:img scaledToSize:CGSizeMake(50, 50)];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        // btn.bounds = CGRectMake( 0, 0, btnImage.size.width, btnImage.size.height );
        [btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchDown];
        [btn setImage:btnImage forState:UIControlStateNormal];
        UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        self.navigationItem.rightBarButtonItem = btnItem;
    });

});

我需要实现与图像相同。我做错了什么,请任何人帮我解决这个问题。

【问题讨论】:

    标签: objective-c uinavigationbar uibarbuttonitem uinavigationitem ios8.3


    【解决方案1】:

    这是 oxigen 的(优秀)答案转换为 Swift 3 / Swift 4:

    override func viewDidLoad()
    {
        super.viewDidLoad()
    
        let url = URL(string: "http://i.stack.imgur.com/EbYBY.jpg")!
        let data = try! Data(contentsOf: url)
        let img = UIImage(data: data)
        let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0))
        imageView.image = img?.withRenderingMode(.alwaysOriginal)
        imageView.layer.cornerRadius = 20.0
        imageView.layer.masksToBounds = true
        let barButton = UIBarButtonItem(customView: imageView)
        navigationItem.setLeftBarButton(barButton, animated: false)
    }
    

    (给定的 URL 现在已经失效了,但是有了正确的 URL 或其他图像,代码应该可以工作。)

    【讨论】:

      【解决方案2】:

      它对我有用:

      - (void)viewDidLoad
       {
       [super viewDidLoad];
        NSString* url = @"http://i.stack.imgur.com/EbYBY.jpg";
       NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL   URLWithString:url]];
      UIImage* img = [UIImage imageWithData:data];
      
      UIImageView* v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
      v.image = img;
      v.layer.masksToBounds = YES;
      v.layer.cornerRadius = 20;
      
      UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:v];
      self.navigationItem.rightBarButtonItem = rightBtn;
      }
      

      【讨论】:

      • 奇怪。正如您在屏幕截图中看到的 - 代码正在运行。也许问题出在您的 NavigationItem 中...您可以向其中添加任何其他 rightButtonItem 吗?
      • 我不确定,我做错了什么。我在另一个视图控制器上使用了相同的代码我遇到了同样的问题。你能建议一些解决方案
      • 1.检查 self.navigationItem ,也许它的大小错误或为零。 2.尝试添加简单的rightItem(只有文字,没有图片)
      • 我尝试使用 UIbarbuttonItem 以编程方式创建导航栏,它工作正常。但我将 UInavigationabar 与用户界面一起放置此代码不起作用
      【解决方案3】:

      你可以用这个让按钮变圆

      UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
      btn.frame = CGRectMake(0,0,100,100)//set your frame's size
      [btn setImage:[UIImage imageNamed:btnImage]  forState:UIControlStateNormal];  
      btn.layer.masksToBounds = NO;
      btn.layer.cornerRadius = btn.bounds.size.width/2;;
      
      UIBarButtonItem * btnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
      [self.navigationItem setRightBarButtonItems:@[btnItem]];
      

      【讨论】:

      • 但我没有得到图像本身
      • 你确定图片来自 URL 吗?
      • 是的,我正在从 URL 获取图像。
      • 将它放在另一个图像视图或其他按钮中并检查它是否正在显示.. 这样您就可以确定图像是否只显示在 UIBarButtonItem 中
      • 是的,我也是这样做的。图像显示在 imageview 中。只有 UIbarbuttonItem 不起作用
      猜你喜欢
      • 2020-08-29
      • 2021-03-21
      • 2017-04-28
      • 2015-10-13
      • 2011-11-09
      • 2013-01-24
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多