1
votes

How to position the UIImageSubview at center in UIScrollview , I am Loading images dynalically in the uiscrollview so when i select any image in the Uiscrollview that Image should display at the center of screen in uiscrollview.

My Image Width is 65 and Screen width is 320 , I am facing problem setting the position of choosen image at the center of screen,
[scrollView1 setContentOffset:CGPointMake(130, 0) animated:YES];
scrollview just scroll to the 130 position. I don't want like this , I want the subview should be displayed at the center.

Please see the screenshot and tell me how to solve this problem.

UIScrollview

1
You can do like this [scrlView setContentOffset:CGPointMake([[UIScreen mainScreen] applicationFrame].size.width/2, 0)]; so the offset will be in the centre of the screen.Gyanendra
Yea exactly ! But the Image which i selected will not be in center , The scrollview will be at center , I want the Image which selected should be displayed at center of screeenkrish
@krish, i expanded the code in my answer, see the update..foundry

1 Answers

0
votes

How about
(pseudocode)
contentOffset.x = image.center.x - (screenwidth/2)

(expanded...)

  UIImageView* imageView  = //imageview that was selected
  CGFloat screenWidth = 320;
  CGPoint offset = scrollView1.contentOffset;
  offset.x = imageView.center.x - screenWidth/2;
  [scrollView1 setContentOffset:offset animated:YES];

I assume you already worked out how to get a pointer to the selected image...

(expanded further...)

Here is a fully-worked out version of the same, using buttons instead of images for ease of selection... try it (just copy into a new single-view project's viewContoller]), you will see that it works. If you are still having problems I expect it is the way you are identifying your image as the selected image for centering. You would need to update your question showing your code...

- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect frame = self.view.bounds;
    frame.size.height = frame.size.height/2;
    self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
    [self.view addSubview:self.scrollView];
    [self.scrollView setContentSize:CGSizeMake(2000,self.scrollView.bounds.size.height)];
    [self.scrollView setDelegate:self];
    for (int i = 1; i < 20; i++) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        NSString* title = [NSString stringWithFormat:@"button %d",i];
        [button setTitle:title forState:UIControlStateNormal];
        button.bounds = CGRectMake(0,0,80,60);
        button.center = CGPointMake(i*100+50,self.scrollView.bounds.size.height/2);
        [button addTarget:self 
                   action:@selector(buttonPressed:)    
        forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
}

- (void)buttonPressed:(UIButton*)sender
{
    CGFloat x = sender.center.x - self.scrollView.bounds.size.width/2.0f;
    [self.scrollView setContentOffset:CGPointMake(x,0) animated:YES];
}