2014-09-20 28 views
0

您好我有一個由UIButtons製成的彈出菜單,我需要在菜單的每個按鈕中的圖像。我需要爲每個單獨的按鈕顯示不同的圖像。總共有6個按鈕。菜單中的按鈕不同的圖像iOS

我怎樣才能做到這一點

- (NSInteger)makeOneButton:(NSString *)title action:(SEL)action top:(NSNumber *)top into:(UIView *)parent 
{ 
    // NOTE: if you want more stuff here, I'd put all of the items in a view (including this button) 

    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setTitle:title forState:UIControlStateNormal]; 
    //[button setImage:[UIImage imageNamed:@"paper.png"] forState:UIControlStateNormal]; 
    //UIButtonTypeCustom 
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:7]]; 
    [parent addSubview:button]; 
    [button mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(top); 
     make.left.equalTo(parent.mas_left); 
     make.width.equalTo(parent); 
     make.height.equalTo([NSNumber numberWithInteger:mCurrentCellHeight]); 
    }]; 
    return mCurrentCellHeight;   
} 
+0

問題是......? – vikingosegundo 2014-09-20 02:21:29

回答

0

添加這個方法是圖像的名字或圖像和其他參數,並添加作爲按鈕的背景下,當u調用此方法添加按鈕還告訴方法,圖像你想設置相應的按鈕。像這樣......

- (NSInteger)makeOneButton:(NSString *)title action:(SEL)action top:(NSNumber *)top imageName:(NSString *)imageName into:(UIView *)parent 
{ 
    // NOTE: if you want more stuff here, I'd put all of the items in a view (including this button) 

    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setTitle:title forState:UIControlStateNormal]; 
    //[button setImage:[UIImage imageNamed:@"paper.png"] forState:UIControlStateNormal]; 
    //UIButtonTypeCustom 
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:7]]; 
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; 
    [parent addSubview:button]; 
    [button mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(top); 
     make.left.equalTo(parent.mas_left); 
     make.width.equalTo(parent); 
     make.height.equalTo([NSNumber numberWithInteger:mCurrentCellHeight]); 
    }]; 
    return mCurrentCellHeight; 
} 
+0

謝謝,但我仍然困惑如何添加圖像 – user1155141 2014-09-20 17:54:05

+0

你想添加一個圖像的按鈕?如果是,那麼你通過這個方法的名稱添加設置爲按鈕的背景圖像。 – user2493047 2014-09-21 10:01:41