自定义背景导致自定义按钮无法显示

时间:2013-12-30 13:32:51

标签: ios uibutton uibackgroundcolor

我在AppDelegate.m

中设置了自定义背景
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];

在我的viewcontroller中,我将自定义按钮添加为子视图:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.mainButton setImage:[UIImage imageNamed:@"aButton"] forState:UIControlStateNormal];
    [self.mainButton setImage:[UIImage imageNamed:@"aButton"] forState:UIControlStateHighlighted];
    [self.mainButton setFrame:CGRectMake(100, 100, 300, 100)];

    [self.view addSubview:self.mainButton];

}

只显示我的背景。如果我删除背景 - 按钮出现。

2 个答案:

答案 0 :(得分:0)

[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"aButton"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"aButton"] forState:UIControlStateHighlighted];
[button setFrame:CGRectMake(100, 100, 300, 100)];

[self.view addSubview:button];

不要忘记扩展名“Button.png”“aButton.jpg”......

答案 1 :(得分:0)

为UIViewController设置模式或只写一行

[self.View bringSubViewToFront:_button]