gpt4 book ai didi

ios - 如何将按钮放在 UIScrollView 的中心?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:10 24 4
gpt4 key购买 nike

我在我的 ScrollView 中生成多个按钮,一个 ScrollView 在 main.storyboard 上有限制,所以它在不同的 iPhone 上的宽度会发生变化。而且我不能让我的按钮到中心。这是我的代码。

@synthesize table,scroll;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
img = @[@"Shirley-Stevens.jpg"];

NSUInteger i;
int buttonWidth=50;
int buttonHeight=50;
int space = 50;
// int xCoord= 100;// (scroll.frame.size.width/2) - buttonWidth - space;
// int Xcoord2= 200;//(scroll.frame.size.width/2) + buttonWidth + space;

int xCoord = (scroll.contentSize.width/2) + buttonWidth + space + buttonWidth + space;
int Xcoord2= (scroll.contentSize.width/2) + buttonWidth + space;
int yCoord=0;
int buffer = 10;
for (i = 1; i <= 100; i++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:[UIImage imageNamed:[img objectAtIndex:0]]forState:UIControlStateNormal];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:aButton];

UIButton *bButton = [UIButton buttonWithType:UIButtonTypeCustom];
[bButton setBackgroundImage:[UIImage imageNamed:[img objectAtIndex:0]]forState:UIControlStateNormal];
bButton.frame = CGRectMake(Xcoord2, yCoord,buttonWidth,buttonHeight );
[bButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:bButton];

yCoord += buttonHeight + buffer;
}
[scroll setContentSize:CGSizeMake(300, yCoord)];
}

- (IBAction)whatever:(UIButton *)sender {
NSLog(@"Button");
}

最佳答案

这是我的代码现在工作正常,基本上问题是,viewDidLoad 在 scrollview 参数更新之前调用。所以练习scrollview最好的方法就是viewDidAppear。

@synthesize table,scroll;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
img = @[@"Shirley-Stevens.jpg",
@"Shirley-Stevens.jpg"];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSUInteger i;
int buttonWidth=50;
int buttonHeight=50;
int space = 50;
int xCoord = (scroll.frame.size.width/2) - buttonWidth - space;
int Xcoord2 = (scroll.frame.size.width/2) + space;
int yCoord=0;
int buffer = 10;
for (i = 1; i <= 100; i++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:[UIImage imageNamed:[img objectAtIndex:0]]forState:UIControlStateNormal];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:aButton];

UIButton *bButton = [UIButton buttonWithType:UIButtonTypeCustom];
[bButton setBackgroundImage:[UIImage imageNamed:[img objectAtIndex:0]]forState:UIControlStateNormal];
bButton.frame = CGRectMake(Xcoord2, yCoord,buttonWidth,buttonHeight );
[bButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:bButton];

yCoord += buttonHeight + buffer;
}
[scroll setContentSize:CGSizeMake(300, yCoord)];
}


- (IBAction)whatever:(UIButton *)sender {
NSLog(@"Button");
}

关于ios - 如何将按钮放在 UIScrollView 的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30588258/

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