Skip to content

Instantly share code, notes, and snippets.

@SwiftArchitect
Last active March 8, 2016 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SwiftArchitect/003be02e89204cad8fea to your computer and use it in GitHub Desktop.
Save SwiftArchitect/003be02e89204cad8fea to your computer and use it in GitHub Desktop.
Swift Autolayout Assistant UIView. Can also be applied to UITableView. See an example use case scenario on http://stackoverflow.com/questions/31907014/ Copyright (c) 2015 Xavier Schott. All rights reserved. MIT License.
//
// ImageViewLayoutAssistant.swift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
@IBDesignable class ImageViewLayoutAssistant: UIImageView {
@IBInspectable var border:Bool = true
@IBInspectable var crosshair:Bool = true
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Border
if border {
self.layer.borderWidth = 1
self.layer.borderColor = ((nil == tintColor)
? UIColor(red: 0x0D/255, green: 0x94/255, blue: 0xFC/255, alpha: 1)
: tintColor).CGColor
}
}
override func drawRect(rect: CGRect) {
let ctx = UIGraphicsGetCurrentContext()
CGContextSaveGState(ctx)
CGContextSetStrokeColorWithColor(ctx, ((nil == tintColor)
? UIColor(red: 0x0D/255, green: 0x94/255, blue: 0xFC/255, alpha: 1)
: tintColor).CGColor)
CGContextSetLineWidth( ctx, 1.0)
// Crosshair
if crosshair {
CGContextMoveToPoint( ctx, 0, 0)
CGContextAddLineToPoint( ctx, bounds.size.width, bounds.size.height)
CGContextMoveToPoint( ctx, 0, bounds.size.height)
CGContextAddLineToPoint( ctx, bounds.size.width, 0)
CGContextStrokePath(ctx)
}
CGContextRestoreGState(ctx)
}
}
//
// TableViewLayoutAssistant.swift
//
// Copyright (c) 2015 Xavier Schott. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
@IBDesignable class TableViewLayoutAssistant: UITableView {
@IBInspectable var border:Bool = true
@IBInspectable var crosshair:Bool = true
override func drawRect(rect: CGRect) {
let ctx = UIGraphicsGetCurrentContext()
CGContextSaveGState(ctx)
CGContextSetStrokeColorWithColor(ctx, ((nil == tintColor)
? UIColor(red: 0x0D/255, green: 0x94/255, blue: 0xFC/255, alpha: 1)
: tintColor).CGColor)
CGContextSetLineWidth( ctx, 1.0)
// Border
if border {
CGContextStrokeRect(ctx, bounds.insetBy(dx: 1, dy: 1))
}
// Crosshair
if crosshair {
CGContextMoveToPoint( ctx, 0, 0)
CGContextAddLineToPoint( ctx, bounds.size.width, bounds.size.height)
CGContextMoveToPoint( ctx, 0, bounds.size.height)
CGContextAddLineToPoint( ctx, bounds.size.width, 0)
CGContextStrokePath(ctx)
}
CGContextRestoreGState(ctx)
}
}
//
// ViewLayoutAssistant.swift
//
// Copyright (c) 2015 Xavier Schott. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
@IBDesignable class ViewLayoutAssistant: UIView {
@IBInspectable var border:Bool = true
@IBInspectable var crosshair:Bool = true
override func drawRect(rect: CGRect) {
let ctx = UIGraphicsGetCurrentContext()
CGContextSaveGState(ctx)
CGContextSetStrokeColorWithColor(ctx, ((nil == tintColor)
? UIColor(red: 0x0D/255, green: 0x94/255, blue: 0xFC/255, alpha: 1)
: tintColor).CGColor)
CGContextSetLineWidth( ctx, 1.0)
// Border
if border {
CGContextStrokeRect(ctx, bounds.rectByInsetting(dx: 1, dy: 1))
}
// Crosshair
if crosshair {
CGContextMoveToPoint( ctx, 0, 0)
CGContextAddLineToPoint( ctx, bounds.size.width, bounds.size.height)
CGContextMoveToPoint( ctx, 0, bounds.size.height)
CGContextAddLineToPoint( ctx, bounds.size.width, 0)
CGContextStrokePath(ctx)
}
CGContextRestoreGState(ctx)
}
}
@SwiftArchitect
Copy link
Author

Visualize the exact layout of UIImageView, UITableView and UIView in Interface Builder or within your application at development stage. A simple tool to help reduce caffeine intake and chronic hair loss.

In action in Interface Builder:
viewlayoutassistant

tableviewlayoutassistant

IBDesignable:

control parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment