2015-12-31 102 views
0

我已經以編程方式創建了酒吧按鈕項目。現在看起來很簡單的文字。我需要設置沒有邊框的背景顏色,也需要以編程方式設置色調顏色。 這裏是我創建欄按鈕項目代碼:爲酒吧按鈕項目和色調顏色添加Bg

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

使用自定義按鈕有自定義控件 下面的代碼可以幫

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]; 
+0

我需要設置左欄按鈕項目。並且我正在更新imagview – user5513630

+0

錯誤'initwithfram:imageview.bound'錯誤,使用button.bound –

+0

不要複製粘貼,根據需要調整它,還可能需要設置色調,背景和前景色,快樂編碼:) –

1

在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; 
+0

我們不應該在APPDELEGATE中添加任何代碼..爲我們規定的規則 – user5513630