代码之家  ›  专栏  ›  技术社区  ›  user5513630

为条形按钮项和色调添加Bg

  •  0
  • user5513630  · 技术社区  · 9 年前

    我用编程方式创建了一个条形按钮项。现在它看起来是一行简单的文本。我需要设置无边框的背景色,还需要通过编程设置淡色。 下面是我创建条形按钮项的代码:

    UIBarButtonItem *callBtn = [[UIBarButtonItem alloc] initWithTitle:@"Call Me" style:UIBarButtonItemStylePlain target:self action:@selector(signUpButtonTapped:)];
        self.navigationItem.rightBarButtonItem = callBtn;
    

    背景色应: #F9F9F9

    色调应为: 00ACC1

    请帮我做

    我需要这样的图像:

    enter image description here

    2 回复  |  直到 9 年前
        1
  •  2
  •   Arslan Asim    9 年前

    使用的自定义按钮可自定义控件 遵循代码可能会有所帮助

     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0,0,60,30);
    [button setBackgroundColor:[UIColor yourColor]];
    [button setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];
    [button setTitle:@"Call Me" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(navigateToDiscussionScreen) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *view = [[UIView alloc] initWithFrame:button.bounds];
    view.backgroundColor = [UIColor clearColor];
    [view addSubview:button];
    UIBarButtonItem *logoBtn = [[UIBarButtonItem alloc]initWithCustomView:view];
    
        2
  •  1
  •   Hamza Ansari    9 年前

    在您的Appdelegate设置栏色调中:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        UIColor *barColor = [UIColor colorWithRed:0 green:172/255.0 blue:193/255.0 alpha:1.0];
        [[UINavigationBar appearance]setBarTintColor: barColor];
        return YES;
    }
    

    现在在视图控制器中:

      UIButton *btn =  [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0,0,80,30);
        [btn addTarget:self action:@selector(signUpButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
        UIColor *barColor = [UIColor colorWithRed:0 green:172/255.0 blue:193/255.0 alpha:1.0];
    
        [btn setTitle:@"Call Me" forState:UIControlStateNormal];
        [btn setTitleColor:barColor forState:UIControlStateNormal];
    
        btn.backgroundColor = [UIColor lightGrayColor];
    
        UIBarButtonItem *callBtn =[[UIBarButtonItem alloc]initWithCustomView:btn];
    
        self.navigationItem.rightBarButtonItem = callBtn;