I'd like to create a UIView programmatically with the exact equivalent of this setup in IB (see screenshot).
Whatever I have attempted myself would not behave the same when rotating and autoresizing the view, so I need a sample from an expert.
Open Xcode ▸ File ▸ New ▸ File ▸ Cocoa Touch class ▸ Add your class name ▸ Select UIView or subclass of UIView under Subclass of ▸ Select language ▸ Next ▸ Select target ▸ Create the source file under your project directory. Programatically create subviews, layout and design your custom view as per requirement.
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super. viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let myNewView=UIView(frame: CGRect(x: 10, y: 100, width: 300, height: 200)) // Change UIView background colour myNewView.
Swift 2.0:
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
Swift 3.0:
view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
Example:
UIView *customView = [[UIView alloc] initWithFrame:frame];
[customView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With