UIView(CompactConstraint)

@interface UIView (CompactConstraint)

UIView (CompactConstraint), a class category from Marco Arment’s CompactConstraint library, is integrated with Cephei. CompactConstraint provides an Auto Layout grammar and methods that are easier to use and understand than UIKit’s built in functions for programmatically adding constraints.

Refer to its readme to learn how to use it. There are two changes to note: most importantly, the methods have an hb_ prefix, and two methods that are marked as deprecated in the original project have been removed.

CompactConstraint is licensed under the MIT License.

  • Add a single constraint with the compact syntax.

    Declaration

    Objective-C

    - (NSLayoutConstraint *)
        hb_addCompactConstraint:(NSString *)relationship
                        metrics:(NSDictionary<NSString *, NSNumber *> *)metrics
                          views:(NSDictionary<NSString *, UIView *> *)views;

    Swift

    func hb_addCompactConstraint(_ relationship: String!, metrics: [String : NSNumber]!, views: [String : UIView]!) -> NSLayoutConstraint!
  • Add any number of constraints. Can also mix in Visual Format Language strings.

    Declaration

    Objective-C

    - (NSArray<NSLayoutConstraint *> *)
        hb_addCompactConstraints:(NSArray<NSString *> *)relationshipStrings
                         metrics:(NSDictionary<NSString *, NSNumber *> *)metrics
                           views:(NSDictionary<NSString *, UIView *> *)views;

    Swift

    func hb_addCompactConstraints(_ relationshipStrings: [String]!, metrics: [String : NSNumber]!, views: [String : UIView]!) -> [NSLayoutConstraint]!
  • And a convenient shortcut for what we always end up doing with the visualFormat call.

    Declaration

    Objective-C

    - (void)hb_addConstraintsWithVisualFormat:(NSString *)format
                                      options:(NSLayoutFormatOptions)opts
                                      metrics:
                                          (NSDictionary<NSString *, NSNumber *> *)
                                              metrics
                                        views:(NSDictionary<NSString *, UIView *> *)
                                                  views;

    Swift

    func hb_addConstraints(withVisualFormat format: String!, options opts: NSLayoutConstraint.FormatOptions = [], metrics: [String : NSNumber]!, views: [String : UIView]!)