如何将按钮放在 UIScrollView 的中心?
How to take button at the center of the UIScrollView?
我在滚动视图中生成多个按钮,滚动视图在 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");
}
设置 UIButton
的 center
点以及 frame
。
例如:设置中心点在UIScrollView
的width/2 & height/2。
我认为你应该只在两行以下进行更改 -
假设您需要两个中间有 space 间隙的按钮。
int xCoord = (scroll.contentSize.width/2) - (space/2) - buttonWidth;
int Xcoord2= (scroll.contentSize.width/2) + (space/2);
希望对您有所帮助。
这是我的代码现在工作正常,基本上问题是,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");
}
我在滚动视图中生成多个按钮,滚动视图在 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");
}
设置 UIButton
的 center
点以及 frame
。
例如:设置中心点在UIScrollView
的width/2 & height/2。
我认为你应该只在两行以下进行更改 -
假设您需要两个中间有 space 间隙的按钮。
int xCoord = (scroll.contentSize.width/2) - (space/2) - buttonWidth;
int Xcoord2= (scroll.contentSize.width/2) + (space/2);
希望对您有所帮助。
这是我的代码现在工作正常,基本上问题是,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");
}