Move view with keyboard using Swift

Here is a solution, without handling the switch from one textField to another: override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillShow:”), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillHide:”), name: UIKeyboardWillHideNotification, object: nil) } func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() { self.view.frame.origin.y -= keyboardSize.height } } func keyboardWillHide(notification: NSNotification) { self.view.frame.origin.y … Read more

How do you add multi-line text to a UIButton?

For iOS 6 and above, use the following to allow multiple lines: button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; // you probably want to center it button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to [button setTitle: @”Line1\nLine2″ forState: UIControlStateNormal]; For iOS 5 and below use the following to allow multiple lines: button.titleLabel.lineBreakMode = UILineBreakModeWordWrap; // you probably want to … Read more

iOS – Dismiss keyboard when touching outside of UITextField

You’ll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the UITextField on it’s selector. The code: In viewDidLoad UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap]; In dismissKeyboard: -(void)dismissKeyboard { [aTextField resignFirstResponder]; } (Where aTextField is the textfield that is responsible for the keyboard) … Read more

How do I size a UITextView to its content?

This works for both iOS 6.1 and iOS 7: – (void)textViewDidChange:(UITextView *)textView { CGFloat fixedWidth = textView.frame.size.width; CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; CGRect newFrame = textView.frame; newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height); textView.frame = newFrame; } Or in Swift (Works with Swift 4.1 in iOS 11) let fixedWidth = textView.frame.size.width let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, … Read more

UITableViewCell, show delete button on swipe

During startup in (-viewDidLoad or in storyboard) do: self.tableView.allowsMultipleSelectionDuringEditing = false Override to support conditional editing of the table view. This only needs to be implemented if you are going to be returning NO for some items. By default, all items are editable. – (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return YES if you want … Read more