Why shouldn’t I subclass a UIButton?

The Cocoa frameworks take the approach that the Object Composition pattern is more appropriate than traditional class hierarchy. In general, this means that there is likely to be a property on UIButton where you can set another object to handle various aspects of the button. This is the preferred way to “customize” how your button … Read more

iOS – add contact into Contacts?

If doing this in iOS 9 or later, you should use the Contacts framework: @import Contacts; You also need to update your Info.plist, adding a NSContactsUsageDescription to explain why your app requires access to contacts. Then, when you then want to programmatically add the contact, then you can do something like: CNAuthorizationStatus status = [CNContactStore … Read more

Having trouble getting UIView sizeToFit to do anything meaningful

The documentation is pretty clear on this. -sizeToFit pretty much calls -sizeThatFits: (probably with the view’s current size as the argument), and the default implementation of -sizeThatFits: does almost nothing (it just returns its argument). Some UIView subclasses override -sizeThatFits: to do something more useful (e.g. UILabel). If you want any other functionality (such as … Read more

Use UIBarButtonItem icon in UIButton

New iOS 13 support SF Symbols now UIImage(systemName: “trash”) for swift 4.2 (call it on main thread) extension UIBarButtonItem.SystemItem { func image() -> UIImage? { let tempItem = UIBarButtonItem(barButtonSystemItem: self, target: nil, action: nil) // add to toolbar and render it let bar = UIToolbar() bar.setItems([tempItem], animated: false) bar.snapshotView(afterScreenUpdates: true) // got image from real … Read more

Multiline UIButton and autolayout

I had the same problem where I wanted my button to grow along with its title. I had to sublcass the UIButton and its intrinsicContentSize so that it returns the intrinsic size of the label. – (CGSize)intrinsicContentSize { return self.titleLabel.intrinsicContentSize; } Since the UILabel is multiline, its intrinsicContentSize is unknown and you have to set … Read more

UIButton setText programmatically

You might want to be more specific next time you ask a question. You can try assign a different tag for each button in interface builder (or the same tag if thats what you need) and then use the following code for (int i = 1 ; i<=10;i++) { UIButton *myButton = (UIButton *)[myView viewWithTag:i]; … Read more

UIButton title changes to default

You need to use setTitle:forState: method instead of setting the titleLabel.text property: [startButton setTitle:@”Start” forState:UIControlStateNormal]; // Normal and highlighted titles do not need to be the same [startButton setTitle:@”Start!” forState:UIControlStateHighlighted]; What happens now is that you set the title in the label that represents the view of the current state, but once the state changes … Read more

Adjust font size of text to fit in UIButton

self.mybutton.titleLabel.minimumScaleFactor = 0.5f; self.mybutton.titleLabel.numberOfLines = 0; <– Or to desired number of lines self.mybutton.titleLabel.adjustsFontSizeToFitWidth = YES; … did the trick, after layoutIfNeeded in viewDidLoad As it turns out, all those must be set to actually adjust the font-size, not just making it fit into the frame. Update for Swift 3: mybutton.titleLabel?.minimumScaleFactor = 0.5 mybutton.titleLabel?.numberOfLines = … Read more

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