In SwiftUI, how to use UIHostingController inside an UIView or as an UIView?

View controllers are not just for the top level scene. We often place view controllers within view controllers. It’s called “view controller containment” and/or “child view controllers”. (BTW, view controller containers are, in general, a great way to fight view controller bloat in traditional UIKit apps, breaking complicated scenes into multiple view controllers.) So, Go … Read more

UIView’s border color in Interface builder doesn’t work?

It’s possible to do this, but it’s not a built-in feature. This is because the Color type in the User Defined Runtime Attributes panel creates a UIColor, but layer.borderColor holds a CGColorRef type. Unfortunately, there’s no way to assign a CGColorRef type in Interface Builder. However, this is possible through a proxy property. See Peter … Read more

Draw line in UIView

Maybe this is a bit late, but I want to add that there is a better way. Using UIView is simple, but relatively slow. This method overrides how the view draws itself and is faster: – (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); // Draw them with a 2.0 stroke width … Read more

iOS – Push viewController from code and storyboard

Objective-C: PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:@”storyBoardIdentifier”]; [self.navigationController pushViewController:newView animated:YES]; Please do notice below points, your storyboard identifier is correct. The root view have navigation Controller. Swift: let newView = self.storyboard?.instantiateViewController(withIdentifier: “storyBoardIdentifier”) as! PlaceViewController self.navigationController?.pushViewController(newView, animated: true)

rotate a UIView around its center but several times

You can use the following animation on your UIView’s layer property. I’ve tested it. Objective-C UIView *viewToSpin = …; CABasicAnimation* spinAnimation = [CABasicAnimation animationWithKeyPath:@”transform.rotation”]; spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI]; [viewToSpin.layer addAnimation:spinAnimation forKey:@”spinAnimation”]; Swift 5.0 let viewToSpin = UIView() // However you have initialized your view let spinAnimation = CABasicAnimation.init(keyPath: “transform.rotation”) spinAnimation.toValue = NSNumber(value: 5.0 * 2.0 … Read more

Creating iPhone Pop-up Menu Similar to Mail App Menu

Creating an Action Sheet in Swift Code has been tested with Swift 5 Since iOS 8, UIAlertController combined with UIAlertControllerStyle.ActionSheet is used. UIActionSheet is deprecated. Here is the code to produce the Action Sheet in the above image: class ViewController: UIViewController { @IBOutlet weak var showActionSheetButton: UIButton! @IBAction func showActionSheetButtonTapped(sender: UIButton) { // Create the … Read more