问题
I have created my project in iOS 8 it works fine and now i am trying to run in iOS9.UIButton is not appearing. I have created a button programmatically, but it is not appearing only in iOS9. It works fine in iOS7 and iOS8. Please see the below code.
UIButton *NewOrderButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[NewOrderButton addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[NewOrderButton setTitle:@"Create" forState:UIControlStateNormal];
NewOrderButton.frame = CGRectMake((self.view.frame.size.width-200)/2,(self.view.frame.size.height+80)/2, 200, 40.0);
[NewOrderButton.layer setBorderWidth:1];
[NewOrderButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
NewOrderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.view addSubview:NewOrderButton];
If i debug this code, then the button is appearing.
回答1:
I ran this code in my environment. just change following
[self.view addSubview:_NewOrderButton];
to
[self.view addSubview:NewOrderButton];
回答2:
i ran it in iOS 9 it works fine..
and change the last line of your code with these..
[self.view addSubview:NewOrderButton];
i hope it helps..
回答3:
You should add your code to
-(void)viewDidLayoutSubviews {
UIButton *NewOrderButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[NewOrderButton addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[NewOrderButton setTitle:@"Create" forState:UIControlStateNormal];
NewOrderButton.frame = CGRectMake((self.view.frame.size.width-200)/2,(self.view.frame.size.height+80)/2, 200, 40.0);
[NewOrderButton.layer setBorderWidth:1];
[NewOrderButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
NewOrderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.view addSubview:NewOrderButton];
}
回答4:
Elavarasan I applied your coding in viedDidLoad and viewDidLayoutSubviews,it works fine.Also please set the background color for the button.Below is the code.Again apply this code in your class.Definitely you will get solution.
UIButton *NewOrderButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[NewOrderButton addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[NewOrderButton setTitle:@"Create" forState:UIControlStateNormal];
NewOrderButton.backgroundColor = [UIColor blueColor];
NewOrderButton.frame = CGRectMake((self.view.frame.size.width/250)/2,(self.view.frame.size.height+80)/2, 200, 40.0);
[NewOrderButton.layer setBorderWidth:1];
[NewOrderButton.layer setBorderColor:[[UIColor greenColor] CGColor]];
NewOrderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.view addSubview:NewOrderButton];
来源:https://stackoverflow.com/questions/33936634/uibutton-is-not-appear-on-view-in-ios-9