Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically create UIView with this setup?

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.

UIView in IB

like image 606
anna Avatar asked Feb 12 '12 13:02

anna


People also ask

How create custom UIView in programmatically in Swift?

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.

How do I create a view programmatically in iOS Swift?

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.


2 Answers

Swift 2.0:

view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

Swift 3.0:

view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
like image 160
Thomás Pereira Avatar answered Oct 15 '22 09:10

Thomás Pereira


Example:

UIView *customView = [[UIView alloc] initWithFrame:frame];
[customView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight];
like image 9
Volodymyr B. Avatar answered Oct 15 '22 08:10

Volodymyr B.