gpt4 book ai didi

ios - 在uiview中动态添加标签,然后将uiview添加到scrollview ios

转载 作者:行者123 更新时间:2023-12-01 18:58:40 26 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,在该应用程序中,我在界面构建器中添加 ScrollView ,然后在 .m 文件中,我在 UIView 中动态添加两个标签,然后在 ScrollView 中动态添加此 UIView,这是在 for 循环中完成的.

我用于 ScrollView 的代码:

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

self.scrollView.contentSize = CGSizeMake(0, self.scrollView.frame.size.height + kbSize.height);
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
self.scrollView.contentSize = CGSizeMake(0, self.scrollView.frame.size.height);
}

添加两个标签的代码是:
UILabel  * label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 + Y,label1.frame.size.width, 21)];
label1.text = @"label1";
#and all label1 propertiies

UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 21 + Y, label2.frame.size.width, 21)];
label2.text = @"label2";

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, view1.frame.size.width, 42)];

CGRect frame;
frame = view1.frame;
[self.scrollView setContentSize:(CGSize){ self.scrollView.contentSize.width,frame.size.height + kbSize.height}];
[self.scrollView addSubview:view1];
[view1 addSubview:label1];
[view1 addSubview:label2];

标签已生成,但问题是我无法向下滚动它们。

我不知道是什么问题。我是新来的。

这是正确的方法还是有其他方法。

希望问题很清楚

提前致谢

最佳答案

这是代码,它会起作用,但我不确定这是否是您正在搜索的内容

- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat height;
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, 320, 30)];
tf.borderStyle = UITextBorderStyleRoundedRect;
[self.scrollView addSubview:tf];
for(NSInteger i=1; i<10;i++) {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gestureMethod:)];
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, (i*100), 320, 100)];
v.backgroundColor = [self randomColor];

UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 320, 20)];
label1.text = @"Label1";

UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, 320, 20)];
label2.text = @"Label2";
[v addSubview:label1];
[v addSubview:label2];

v.tag = i;
[v addGestureRecognizer:tapGesture];
[self.scrollView addSubview:v];
height+=100;
}
height+=100;
self.scrollView.scrollEnabled = YES;
self.scrollView.contentSize = CGSizeMake(320, height);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];

/// the gesture selector method down HERE

// Do any additional setup after loading the view, typically from a nib.
}
- (void)gestureMethod:(id)sender {
UIView *view = [(UITapGestureRecognizer *)sender view];
//treat the gesture inside the view based on the tag.
NSLog(@"The tap is in the view with the tag: %ld",(long)view.tag);
}

-(UIColor *)randomColor
{
NSInteger aRedValue = arc4random()%255;
NSInteger aGreenValue = arc4random()%255;
NSInteger aBlueValue = arc4random()%255;

UIColor *randColor = [UIColor colorWithRed:aRedValue/255.0f green:aGreenValue/255.0f blue:aBlueValue/255.0f alpha:1.0f];
return randColor;
}

- (void)keyboardWillShow:(NSNotification*)notification {

NSDictionary* userInfo = [notification userInfo];

//scroll scrollView to bottom
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height, 0);
}

- (void)keyboardWillHide:(NSNotification*)notification {
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);


}

这是整个代码。对我来说它有效,但在 Storyboard 中,您必须向 ScrollView 添加约束。从scrollView到superview的Leading,Top,Trailing,Bottom约束都等于0。并且您使用键盘外观的通知。试试看,如果您还有问题,请告诉我。

关于ios - 在uiview中动态添加标签,然后将uiview添加到scrollview ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24568807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com