GithubHelp home page GithubHelp logo

inzan / center-vfl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evgenyneu/center-vfl

0.0 2.0 0.0 168 KB

Centering a view in a super view with Visual Format Language using Auto Layout in iOS/Swift

Swift 100.00%

center-vfl's Introduction

Centering a view in a super view with visual format language using Auto Layout in iOS/Swift

This is an example of centeing a label in its view using Visual Format Language. Tested in iOS 7, 8 and 9.

Here is the code from view controller that does the trick:

// Center horizontally
var constraints = NSLayoutConstraint.constraintsWithVisualFormat(
  "V:[superview]-(<=1)-[label]",
  options: NSLayoutFormatOptions.AlignAllCenterX,
  metrics: nil,
  views: ["superview":view, "label":label])

view.addConstraints(constraints)

 // Center vertically
constraints = NSLayoutConstraint.constraintsWithVisualFormat(
  "H:[superview]-(<=1)-[label]",
  options: NSLayoutFormatOptions.AlignAllCenterY,
  metrics: nil,
  views: ["superview":view, "label":label])

view.addConstraints(constraints)

Centering in superview with VFL

How it works

This code is something like special theory of relativity. I can not say I completely understand it. But it seems to work.

We use two visual format strings:

  1. @"V:[superview]-(<=1)-[label]" with AlignAllCenterX option
  2. @"H:[superview]-(<=1)-[label]" with AlignAllCenterY option

It aligns X and Y centres of the label and its superview. The (<=1) inequality constraints are needed to allow those centering constraints do their job. If we had just [superview][label] it would probably stick the edges of the label and its superview together.

Feedback is welcome

If you need help or want to improve this technique feel free to create a pull request, or comment on this stackoverflow answer.

center-vfl's People

Contributors

evgenyneu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.