Ian Vink
Ian Vink

Reputation: 68750

UIScrollView - Add Subview so that it's centered Horizontal and Vertical

How do I add, in code, a UIImageView to a UIScrollView so that it appears in the center.

The scroll view fills a large area, the UIImage view is about 40% the size on average.

Upvotes: 3

Views: 3326

Answers (2)

user456236
user456236

Reputation:

What about something around the lines of...

int size = 20;

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((scrollView.center.x - (size / 2)), (scrollView.center.y - (size / 2)), size, size)];

[scrollView addSubview:imageView];

Upvotes: 1

user745098
user745098

Reputation:

How about imageView.center = scrollView.center

Upvotes: 6

Related Questions