Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Your (Forgotten) Friend Inside Xcode - Interface Builder

Your (Forgotten) Friend Inside Xcode - Interface Builder

Mathias Nagler

May 24, 2016
Tweet

Other Decks in Programming

Transcript

  1. I can't imagine how anybody is able to build an

    app with IB and not going crazy. — Me, about 3 years ago
  2. - (void)layoutSubviews { [super layoutSubviews]; CGFloat inset = ContentPadding().left +

    20; CGSize contentSize = CGRectInset(self.bounds, inset, 0).size; CGRect frame = self.imageView.frame; frame.origin.x = floorf((self.bounds.size.width - frame.size.width) / 2); frame.origin.y = floorf(self.center.y - frame.size.height); self.imageView.frame = frame; _titleLabel.frame = (CGRect){ .origin = {inset, CGRectGetMaxY(frame)}, .size = {contentSize.width, 80} }; }
  3. Reusable "Use UITableViewCells in a type-safe way, without manipulating their

    String-typed reuseIdentifiers" https://github.com/AliSoftware/Reusable
  4. Xib Loading let nib = UINib(nibName: "RandomView", bundle: nil) let

    view = nib.instantiateWithOwner(nil, options: nil).first as? RandomView ! import Reusable class RandomView: UIView, NibLoadable { // The rest of the view code goes here } let view = RandomView.loadFromNib()
  5. Xib Referencing: Option A // RandomView.swift override func awakeAfterUsingCoder(aDecoder: NSCoder)

    -> AnyObject? { if subviews.count > 0 { return super.awakeAfterUsingCoder(aDecoder) } let view = self.dynamicType.loadFromNib() // Transfer some common properties view.translatesAutoresizingMaskIntoConstraints = translatesAutoresizingMaskIntoConstraints // frame, backgroundColor, alpha, ... // Transfer Constraints for placeholderConstraint in constraints { // Check out the example project } return view }
  6. Xib Referencing: Option B // RandomView.swift required init?(coder aDecoder: NSCoder)

    { super.init(coder: aDecoder) let nibView = self.dynamicType.loadFromNib() nibView.translatesAutoresizingMaskIntoConstraints = false addSubview(nibView) nibView.leadingAnchor.constraintEqualToAnchor(leadingAnchor).active = true nibView.trailingAnchor.constraintEqualToAnchor(trailingAnchor).active = true nibView.topAnchor.constraintEqualToAnchor(topAnchor).active = true nibView.bottomAnchor.constraintEqualToAnchor(bottomAnchor).active = true }
  7. enum AppColors { static func blackColor() -> UIColor { return

    UIColor(red: 0.000000, green: 0.000000, blue: 0.000000, alpha: 1.000000) } static func grayColor() -> UIColor { return UIColor(red: 0.265146, green: 0.265153, blue: 0.265149, alpha: 1.000000) } }
  8. Conclusion → Keep your IB-Files small and modular → Use

    IB where it makes sense ... → ... and code where it does not