如何在swift的导航项栏中调整图像图标的大小?

时间:2018-02-05 06:59:36

标签: ios swift navigationbar

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

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

enter image description here

2 个答案:

答案 0 :(得分:2)

您必须为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)

谢谢

答案 1 :(得分:0)

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

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;

谢谢