【问题标题】:How to add Background image for Custom UIToolBar如何为自定义 UIToolBar 添加背景图像
【发布时间】:2013-12-29 18:47:36
【问题描述】:

我正在使用下面的代码将背景图像添加到自定义 UI 工具栏,其中有 4 个自定义 UIBarButton。我想为工具栏添加背景图像,我搜索了很多东西并尝试了但没有任何效果。谁能告诉我如何为我指定的框架添加自定义 UIToolBar 的背景图像。

UIToolBar *toolBar =[[UIToolBar alloc]init];


-(void)layoutSubviews{

 CGRect frame;
  frame = CGRectMake(20 ,90, 150, 30);
    toolBar.frame = frame;
UIImage* toolbarImage = [UIImage imageNamed: @"toolbar_background.png"];
[[UIToolbar appearance] 
    setBackgroundImage: toolbarImage 
    forToolbarPosition: UIToolbarPositionAny
    barMetrics: UIBarMetricsDefault];
}

上面的代码对我来说不起作用,因为我的是自定义工具栏以编程方式添加,而且我正在根据要求添加它。

【问题讨论】:

    标签: ios objective-c uitoolbar


    【解决方案1】:

    我遇到了同样的问题,现在可以正常工作了。你可以试试,如果效果好的话, 1.首先我将工具栏隐藏为NO。

    [self.navigationController setToolbarHidden:NO  animated:YES];
    

    2.addin Image to UItoolbar UIImage *toolbarImage = [[UIImage imageNamed:@"search_BottomBar1.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

    [[UIToolbar appearance]setBackgroundImage:toolbarImage forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance]setTintColor:[UIColor grayColor]];
    
    1. 我在其中添加了条形按钮。
      UIBarButtonItem *button2 = [[UIBarButtonItem alloc]initWithTitle:@"My Place" 样式:UIBarButtonItemStyleBordered 目标:自我动作:@selector(clickedButton1)];

      NSArray *itemsN = [NSArray arrayWithObjects:button0,button1,button2, nil]; [self setToolbarItems:itemsN animated:NO];

    试试这个,希望它会工作..

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多