The idea is user can click a button Add Country, Country picker will come up, and whatever user selects from picker will be added as label/buttons (USA delete) under Add Country button. These new delete buttons, if clicked will remove themselves and their label.
I can already get the value from Country picker and create a button using the code from here How do I create a basic UIButton programmatically?
My problem is the button is just being positioned on top of existing elements. I would like created buttons to be under the Add Country button or under previously created buttons. Whatever element was previously below Add Country button should move down.
I don't even know what search terms I should use to google this. I've thought of using uitableview to contain the newly created buttons and it would expand somehow but I've had no luck with this either. I am using storyboard by the way.
EDIT: Added relevant code:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// get text of selected option
NSString *pickedText;
if ([pickerView tag] == 201) {
pickedText = [arrCountries objectAtIndex:row];
}
else if([pickerView tag] == 202) {
pickedText = [arrLanguages objectAtIndex:row];
}
// create button
UIButton *countryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[countryButton addTarget:self
action:@selector(removeCountry:)
forControlEvents:UIControlEventTouchUpInside];
[countryButton setTitle:pickedText forState:UIControlStateNormal];
countryButton.frame = CGRectMake(0, 210.0, 160.0, 40.0);
[self.view addSubview:countryButton];
// hide picker
[countryPicker setHidden:YES];
[languagePicker setHidden:YES];
}
- (void)removeCountry:(UIButton *)countryButton
{
[countryButton removeFromSuperview];
}
EDIT2: Updated code where elements are moving down except for previously dynamically added button.
// create button
UIButton *countryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[countryButton addTarget:self
action:@selector(removeCountry:)
forControlEvents:UIControlEventTouchUpInside];
[countryButton setTitle:pickedText forState:UIControlStateNormal];
CGRect newFrame = addCountryButton.frame;
newFrame.origin.y = newFrame.origin.y + addCountryButton.frame.size.height + countryButton.frame.size.height;
countryButton.frame = newFrame;
[self.view addSubview:countryButton];
// move elements down
[elementsBelowAddCountry enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
CGRect newFrame = [obj frame];
newFrame.origin.y = newFrame.origin.y + countryButton.frame.size.height;
[obj setFrame:newFrame];
NSLog(@"object %@", obj);
}];
[elementsBelowAddCountry setByAddingObject:countryButton];
// this doesn't work. new countryButton is set on top of previous countryButton