UIViewController中没有显示UIButton

时间:2022-06-01 19:59:25

I have set up my button as so:

我设置了按钮如下:

CGRect button1Frame = CGRectMake(200, 200, 100, 100);
self.button1 = [[UIButton alloc] initWithFrame:button1Frame];
[self.button1 addTarget:self action:@selector(goToController1:)
 forControlEvents:UIControlEventTouchDown];
self.button1.userInteractionEnabled = YES;
self.button1.opaque = YES;
self.button1.backgroundColor = [UIColor redColor];
[self.button1 setTitle:@"B1" forState:UIControlStateNormal];
[self.view addSubview:self.button1];

with my sender:

与我发送者:

- (void) goToController1:(id)sender {
    NSLog(@"Pressed");
}

and my view controller has been initialized properly. What would cause my button not to show up? I have checked to make sure everything is being called properly.

我的视图控制器已经正确初始化。什么会使我的按钮不出现?我已经检查过,以确保所有的东西都被正确调用。

1 个解决方案

#1


5  

You could try this:

你可以试试这个:

button = [UIButton buttonWithType: UIButtonTypeSystem];

And then set the frame like this:

然后像这样设置框架:

button.frame = CGRectMake(0,0,20,20);

The UIButton buttonWithType: method fixed my problem at one point. Not sure if it will make a difference in this case though. Just something to try.

方法在某一点上修正了我的问题。但不确定这是否会对这个案子有所影响。只是尝试的东西。

#1


5  

You could try this:

你可以试试这个:

button = [UIButton buttonWithType: UIButtonTypeSystem];

And then set the frame like this:

然后像这样设置框架:

button.frame = CGRectMake(0,0,20,20);

The UIButton buttonWithType: method fixed my problem at one point. Not sure if it will make a difference in this case though. Just something to try.

方法在某一点上修正了我的问题。但不确定这是否会对这个案子有所影响。只是尝试的东西。