0

i want to align a picker view (pick) in centre of superview(i.e self.view) in all screen sizes.(iphone-4-5-6-6+,ipad-min-air-pro).

i have aligned it in centre in iphone6 using this code

pick.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(<=112)-[pick(150)]-(<=112)-|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:NSDictionaryOfVariableBindings(pick)]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(<=258)-[pick(150)]-(<=258)-|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:NSDictionaryOfVariableBindings(pick)]];

General calculation i did is:-

  • Subtract half width/height of subview to half width/height of superview.

How to align this view for every screen size without using #define width and height of screen. Also what calculation you did to obtain the result? Also i would appreciate if the constraints are set using visual format language. It should have same alignment for all iOS device screen's. Thank You..

iSwift
  • 314
  • 3
  • 17
  • Have you seen this thread : http://stackoverflow.com/questions/12873372/centering-a-view-in-its-superview-using-visual-format-language? Also, do you necessarily need to use VFL? Using a library such as Masonry (https://github.com/SnapKit/Masonry) would make it dead simple (essentially, one line) – Losiowaty Sep 29 '15 at 13:20
  • As mention in the read me file:- Masonry is in a bugfix only state as more developers will be moving to Swift. Going forward we recommend using SnapKit as it supports Swift and provides better type safety with a simpler API. – iSwift Sep 30 '15 at 06:32
  • also i have read the link provided, in it a guy(CopperCash) gives a solution to add the view in centre using alignment properties and adding it into dictionary object. but how to do it? – iSwift Sep 30 '15 at 06:38
  • Have you tried using his constraints? His `view` variable is essentialy your `pick` variable. – Losiowaty Sep 30 '15 at 09:54
  • Yes,his code is missing something (centreXView/centerYView identifier missing),block NSString unused. So, what to declare as centerX/YView? and where to use the NSString Block – iSwift Oct 02 '15 at 11:33

0 Answers0