想法是用户可以单击按钮添加国家/地区,国家选择器将出现,并且任何用户从选择器中选择的任何内容都将添加为添加国家/地区按钮下的标签/按钮(美国删除)。这些新的删除按钮,如果点击将删除自己和他们的标签。
我已经可以从国家/地区选择器中获取值并使用此处的代码创建一个按钮How do I create a basic UIButton programmatically?
我的问题是按钮只是位于现有元素的顶部。我想创建按钮在添加国家/地区按钮下或在以前创建的按钮下。无论以前在“添加国家/地区”按钮下面是什么元素都应向下移动。
我甚至不知道我应该使用什么搜索词来谷歌这个。我曾经想过使用uitableview来包含新创建的按钮,它会以某种方式扩展,但我也没有运气。顺便说一下,我正在使用故事板。
编辑:添加了相关代码:
-(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:更新了代码向下移动的代码,但以前动态添加的按钮除外。
// 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
答案 0 :(得分:1)
我也喜欢tableView选项,它很干净。以下是另外两种选择:
您可以在故事板中创建视图(A)的精确副本(B),包括B中的新子视图(countryButton),其他所有内容都向下移动。然后,当用户选择在A中添加国家时,使用淡入淡出(不是令人困惑的推送)将(转移)转换为B。维护问题是,无论何时更新视图A,您都必须记住更新B。这个选项很好用的是你可以根据需要改变线之间的间距(以便不在屏幕上移动任何东西)。
另一种单视图替代方法是,如果单击addCountry,则维护一组要移动的项目(在viewDidLoad中构建列表)。选中后,使用enumerateObjectsUsingBlock枚举对象:使用以下内容:
CGRect newFrame = self.frame;
newFrame.origin.y = newFrame.origin.y + countryButton.frame.size.height + 10;
self.frame = newFrame;
如果删除countryButton,除了减少y之外,请执行相同的操作。
我有点像第二种选择。这也很好,因为如果你将枚举放入一个动画块,它将看起来很光滑。
享受,
达明
答案 1 :(得分:0)
UIButton *yourOriginalButtom;
UIButton *newButton = [[UIButton alloc] initWithFrame:yourFrame]; /* or use UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom]; or UIButtonTypeRoundedRect */
newButton.center = CGPointMake(yourOriginalButtom.center.x, yourOriginalButtom.center.y+newButton.frame.size.height/2 + 15);
[newButton setTitle:@"New button" forState:UIControlStateNormal];
newButton.backgroundColor = [UIColor whiteColor]; // this will only work on custom buttons
[self.view addSubview:newButton];
答案 2 :(得分:0)
我可以建议两种方法来做到这一点:
最简单的方法是将它添加到tableview上,这样每次用户添加一个国家时,你都会对表数据进行重新加载,因此它会立即出现在表中,我更喜欢这个,因为表视图是支持编辑并删除行功能。
你可以存储最后一个控件的y点(在你的情况下是标签),所以下次用户添加一个国家时,它很容易计算下一个标签的框架应该在哪里。