【问题标题】:how to resize image icon in navigation item bar in swift?如何快速调整导航项栏中的图像图标大小?
【发布时间】:2018-02-05 06:59:35
【问题描述】:

我有 2 个导航栏项目,但尺寸太大,我想把它变小。我如何管理它们的大小?我应该在界面生成器中编辑它们还是应该在viewDidLoad 中以编程方式管理它们?

我已经给出了图像资源中所需的所有图像大小,但它不起作用

【问题讨论】:

标签: ios swift navigationbar


【解决方案1】:

您必须为navigationItem 采取自定义视图

按下按钮并根据您的要求提供框架

为按钮设置图片

customView 的按钮分配给UIBarButtonItem

试试下面的代码..它可能会帮助你

    let customButton = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 30, height: 30))
    customButton.setImage(UIImage.init(named:"imageName"), for: .normal)
    self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: customButton)

谢谢

【讨论】:

    【解决方案2】:

    要在条形按钮中使用调整大小的图像,下面的代码将为您提供帮助:

    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [customView setBackgroundColor:[UIColor clearColor]];
    
    CGRect iconRect = CGRectMake(((40 - 28)/2), ((40 - 28)/2), 28, 28);
    UIImageView *imageIcon = [[UIImageView alloc] initWithFrame: iconRect];
    imageIcon = [UIImage imageNamed:imageName];
    [customView addSubview: imageIcon];
    
    UIButton *tranparentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [subscribeButton setFrame:CGRectMake(0, 0, 40, 40)];
    [customView addSubview: tranparentButton];
    [tranparentButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView: customView];
    
    self.navigationItem.rightBarButtonItem = rightBarButton;
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2016-08-18
      • 1970-01-01
      • 2011-07-19
      • 2017-11-20
      • 1970-01-01
      • 2020-12-07
      • 2017-02-21
      • 1970-01-01
      相关资源
      最近更新 更多