ios - Evenly space UIImageViews within UIScrollView -
i have uiscrollview in vc containing 9 image views size of 25px x 25 px on iphone 5s spaced in centre of scroll view. when ran on iphone 6 pushed on left, not stretching evenly across screen, due screen size.
is there way can make them evenly spaced on both screen sizes?
code below:
// set scrollview nslog(@"%f", self.view.frame.size.width); uiscrollview *scrollview = [[uiscrollview alloc] initwithframe:cgrectmake(25, 200, self.view.frame.size.width - 50, 30)]; scrollview.alwaysbouncehorizontal = yes; nslog(@"%f", scrollview.frame.size.width); uiview *view = [[uiview alloc] initwithframe:cgrectmake(0, 0, scrollview.frame.size.width, scrollview.frame.size.height)]; [scrollview addsubview:view]; uiimage *star = [uiimage imagenamed:@"badge_star"]; uiimageview *imgview = [[uiimageview alloc] initwithimage:star]; imgview.frame = cgrectmake(2.5, 5, 25, 25); [view addsubview:imgview]; uiimage *target= [uiimage imagenamed:@"badge_target"]; uiimageview *targetimageview = [[uiimageview alloc] initwithimage:target]; targetimageview.frame = cgrectmake(32.5, 5, 25, 25); [view addsubview:targetimageview]; uiimage *crown = [uiimage imagenamed:@"badge_crown"]; uiimageview *crownimageview = [[uiimageview alloc] initwithimage:crown]; crownimageview.frame = cgrectmake(62.5, 5, 25, 25); [view addsubview:crownimageview]; uiimage *scissors = [uiimage imagenamed:@"badge_scissors"]; uiimageview *scissorsimageview = [[uiimageview alloc] initwithimage:scissors]; scissorsimageview.frame = cgrectmake(92.5, 5, 25, 25); [view addsubview:scissorsimageview]; uiimage *thumbsup = [uiimage imagenamed:@"badge_thumbs_up"]; uiimageview *thumbsupimageview = [[uiimageview alloc] initwithimage:thumbsup]; thumbsupimageview.frame = cgrectmake(122.5, 5, 25, 25); [view addsubview:thumbsupimageview]; uiimage *trophy = [uiimage imagenamed:@"badge_trophy"]; uiimageview *trophyimageview = [[uiimageview alloc] initwithimage:trophy]; trophyimageview.frame = cgrectmake(152.5, 5, 25, 25); [view addsubview:trophyimageview]; uiimage *medal = [uiimage imagenamed:@"badge_medal"]; uiimageview *medalimageview = [[uiimageview alloc] initwithimage:medal]; medalimageview.frame = cgrectmake(182.5, 5, 25, 25); [view addsubview:medalimageview]; uiimage *heart = [uiimage imagenamed:@"badge_heart"]; uiimageview *heartimageview = [[uiimageview alloc] initwithimage:heart]; heartimageview.frame = cgrectmake(212.5, 5, 25, 25); [view addsubview:heartimageview]; uiimage *coathanger = [uiimage imagenamed:@"badge_coathanger"]; uiimageview *coathangerimageview = [[uiimageview alloc] initwithimage:coathanger]; coathangerimageview.frame = cgrectmake(242.5, 5, 25, 25); [view addsubview:coathangerimageview]; [self.view addsubview:scrollview]; }
that because hard coding start point of scroll view. have 2 option;
1. increase spacing between each image or
2. change start point of scroll view
opting 2nd option relatively easy in code. use below code scroll view frame.
float scrollviewwidth = 25*9+5*8+2*2.5; // cross check one. float xscrollpoint = (self.view.frame.size.width - scrollviewwidth)/2 uiscrollview *scrollview = [[uiscrollview alloc] initwithframe:cgrectmake(xscrollpoint, 200, scrollviewwidth, 30)];
Comments
Post a Comment