ios - 如何在 UIScrollView 的中心获取按钮?
<p><p>我在我的 ScrollView 中生成多个按钮, ScrollView 在 main.storyboard 有约束,因此它的宽度在不同的 iphone 上会发生变化。我不能让我的按钮到中心。这是我的代码。</p>
<pre><code>@synthesize table,scroll;
- (void)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 = ;
]forState:UIControlStateNormal];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
;
;
UIButton *bButton = ;
]forState:UIControlStateNormal];
bButton.frame = CGRectMake(Xcoord2, yCoord,buttonWidth,buttonHeight );
;
;
yCoord += buttonHeight + buffer;
}
;
}
- (IBAction)whatever:(UIButton *)sender {
NSLog(@"Button");
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这是我的代码现在可以正常工作,基本上问题是,在 ScrollView 的参数更新之前调用了 viewDidLoad。所以练习scrollview最好的方法就是viewDidAppear。 </p>
<pre><code>@synthesize table,scroll;
- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
img = @[@"Shirley-Stevens.jpg",
@"Shirley-Stevens.jpg"];
}
- (void)viewDidAppear:(BOOL)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 = ;
]forState:UIControlStateNormal];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
;
;
UIButton *bButton = ;
]forState:UIControlStateNormal];
bButton.frame = CGRectMake(Xcoord2, yCoord,buttonWidth,buttonHeight );
;
;
yCoord += buttonHeight + buffer;
}
;
}
- (IBAction)whatever:(UIButton *)sender {
NSLog(@"Button");
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在 UIScrollView 的中心获取按钮?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/30588258/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/30588258/
</a>
</p>
页:
[1]