Skip to content

Instantly share code, notes, and snippets.

View wrapper.swift
// avoid ambiguous position by using wrapper
let wrapper = UIView()
let view = HeaderView()
view.translatesAutoresizingMaskIntoConstraints = false
wrapper.addSubview(view)
NSLayoutConstraint.activate([
view.leftAnchor.constraint(equalTo: wrapper.leftAnchor),
view.rightAnchor.constraint(equalTo: wrapper.rightAnchor),
view.topAnchor.constraint(equalTo: wrapper.topAnchor),
view.bottomAnchor.constraint(equalTo: wrapper.bottomAnchor),
View activate.swift
NSLayoutConstraint.activate([
play.rightAnchor.constraint(
equalTo: guide_mark.leftAnchor,
constant: -8
)
])
View addConstraints.swift
// add constraints through Autolayout Visual Format Language
view.addConstraints(
NSLayoutConstraint.constraints(
withVisualFormat: "H:|-50-[button]",
options: .alignAllTop,
metrics: nil,
views: objects
)
)
View constraints.swift
// UIViewController
let constraints = [
view.centerXAnchor.constraint(equalTo: superview.centerXAnchor),
view.centerYAnchor.constraint(equalTo: superview.centerYAnchor),
view.widthAnchor.constraint(equalTo: superview.widthAnchor)
view.heightAnchor.constraint(equalTo: superview.heightAnchor),
]
NSLayoutConstraint.activate(constraints)
View uiview+extension.h
/// UIView.h
@class NSLayoutXAxisAnchor,NSLayoutYAxisAnchor,NSLayoutDimension;
@interface UIView (UIViewLayoutConstraintCreation)
/* Constraint creation conveniences. See NSLayoutAnchor.h for details.
*/
@property(nonatomic,readonly,strong) NSLayoutXAxisAnchor *leadingAnchor API_AVAILABLE(ios(9.0));
@property(nonatomic,readonly,strong) NSLayoutXAxisAnchor *trailingAnchor API_AVAILABLE(ios(9.0));
// more anchors...
@end
View nudge_in_swift.py
""" File: nudge.py
An lldb Python script to add a nudge command.
Add to ~/.lldbinit:
command script import ~/path/to/nudge.py
Usage:
(lldb) nudge x-offset y-offset [view]
View PurePerson.swift
class Person {
var age: Int
init(age: Int) {
self.age = age
}
}
View Person.swift
class Person: NSObject {
@objc dynamic var age: Int
init(age: Int) {
self.age = age
super.init()
}
}
View NSKeyValueObserverRegistration.h
@interface NSObject(NSKeyValueObserverRegistration)
/* Register or deregister as an observer of the value at a key path relative to the receiver.
The options determine what is included in observer notifications and when they're sent, as described above,
and the context is passed in observer notifications as described above. You should use
-removeObserver:forKeyPath:context: instead of -removeObserver:forKeyPath: whenever possible because it allows
you to more precisely specify your intent. When the same observer is registered for the same key path multiple times,
but with different context pointers each time, -removeObserver:forKeyPath: has to guess at the context pointer when
deciding what exactly to remove, and it can guess wrong.
*/
View shouldNotAutoRotate.mm
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight);
}