简体   繁体   中英

How to add a UIButton to a UIButton

How do I add a UIButton on top of a UIButton?

Such that if I move the the parent UIButton the one on top moves as well.

UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
testButton.backgroundColor = [UIColor redColor];

UIButton *smallerButton = [UIButton buttonWithType:UIButtonTypeCustom];
smallerButton.backgroundColor = [UIColor greenColor];
[testButton addSubview:smallerButton];

[self.view addSubview:testButton];
[oneButton addSubview:otherButton];

请记住, UIButtonUIView的子类......

UIButton *parentBtn;
UIButton *childBtn;

parentBtn = [UIButton buttonWithType: // init the button];
childBtn = //same for child btn

[self.view addSubview:parentBtn]; //add the parent button to the view
[parentBtn addSubview:childBtn]; //add the child button on top of the parent button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM