开发者

UIButton with CGRect doesn't appear in UIScrollView

开发者 https://www.devze.com 2023-03-15 19:36 出处:网络
- (CGPoint)getImageOrigin:(NSInteger)imageNumber { CGFloat leftInset = 40; CGFloat xOffsetBetweenOrigins = 100;
- (CGPoint)getImageOrigin:(NSInteger)imageNumber {
CGFloat leftInset = 40;
CGFloat xOffsetBetweenOrigins = 100;
CGFloat topInset = 40;
CGFloat yOffsetBetweenOrigins = 100;
int numPerRow = 3开发者_如何学C;
CGFloat x = leftInset + (xOffsetBetweenOrigins * (imageNumber % numPerRow));
CGFloat y = topInset + (yOffsetBetweenOrigins * floorf(imageNumber / numPerRow));
CGPoint imageOrigin = CGPointMake(x, y);
return imageOrigin;
}

-(void)viewDidLoad{
UIButton *zenbutton2 =[UIButton buttonWithType:UIButtonTypeCustom];
CGRect newFrame = zenbutton2.frame;
newFrame.origin = [self **getImageOrigin**:i];
zenbutton2.frame = newFrame;
[scrollView addSubview:zenbutton2];  
}

followed the method here : laying out images in UIScrollView automatically

but my UIButton doesnt appear in my scrollView , any idea why?


Your UIButton is of UIButtonTypeCustom. So you should set a background color or Image to see the button.

[button setImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal];

or

button.backgroundColor = [UIColor redColor]

Try this.


You need to set scrollVew's content size after adding button's.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号