Set the maximum character length of a UITextField

While the UITextField class has no max length property, it’s relatively simple to get this functionality by setting the text field’s delegate and implementing the following delegate method: Objective-C – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // Prevent crashing undo bug – see note below. if(range.length + range.location > textField.text.length) { return NO; } NSUInteger … Read more

Creating an abstract class in Objective-C

Typically, Objective-C class are abstract by convention only—if the author documents a class as abstract, just don’t use it without subclassing it. There is no compile-time enforcement that prevents instantiation of an abstract class, however. In fact, there is nothing to stop a user from providing implementations of abstract methods via a category (i.e. at … Read more

How can I set the title of a UIButton as left-aligned?

Set the contentHorizontalAlignment: // Swift emailBtn.contentHorizontalAlignment = .left; // Objective-C emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; You might also want to adjust the content left inset otherwise the text will touch the left border: // Swift 3 and up: emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0); // Objective-C emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

How to hide UINavigationBar 1px bottom line

For iOS 13: Use the .shadowColor property If this property is nil or contains the clear color, the bar displays no shadow For instance: let navigationBar = navigationController?.navigationBar let navigationBarAppearance = UINavigationBarAppearance() navigationBarAppearance.shadowColor = .clear navigationBar?.scrollEdgeAppearance = navigationBarAppearance For iOS 12 and below: To do this, you should set a custom shadow image. But for … Read more

How do I create a basic UIButton programmatically?

Here’s one: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@”Show View” forState:UIControlStateNormal]; button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view addSubview:button];

Should IBOutlets be strong or weak under ARC?

WARNING, OUTDATED ANSWER: this answer is not up to date as per WWDC 2015, for the correct answer refer to the accepted answer (Daniel Hall) above. This answer will stay for record. Summarized from the developer library: From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should … Read more

@synthesize vs @dynamic, what are the differences?

@synthesize will generate getter and setter methods for your property. @dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass or will be provided at runtime). Uses for @dynamic are e.g. with subclasses of NSManagedObject (CoreData) or when you want to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)