How to click a button programmatically?
To fire an event programmatically you need to call sendActionsForControlEvent button.sendActionsForControlEvents(.TouchUpInside) — Swift 3 button.sendActions(for: .touchUpInside)
To fire an event programmatically you need to call sendActionsForControlEvent button.sendActionsForControlEvents(.TouchUpInside) — Swift 3 button.sendActions(for: .touchUpInside)
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 …
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]; …
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 …
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 = …
Try to Override the UIButton with the following Method.. and just change the backgroud color of button when its in highlighted state. – (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; if (highlighted) { self.backgroundColor = [UIColor Your Customcolor]; } else{ self.backgroundColor = [UIColor Your DefaultColor]; } } Try it..hope it helps
You said you set a custom image for the UIControlStateHighlighted state. This should disable the default behaviour. If you still have problems you can disable this effect by setting the adjustsImageWhenHighlighted property to NO and use whatever custom effect you want.
Image overrides title, you need to make the image a background image to show the title. [button setBackgroundImage:[UIImage imageNamed:@”left_halfscreen_button.png”] forState:UIControlStateNormal];
Swift 4.x version of Kubba’s answer: Need to Update Line Break as Clip/WordWrap/ in Interface builder to corresponding buttons. class ResizableButton: UIButton { override var intrinsicContentSize: CGSize { let labelSize = titleLabel?.sizeThatFits(CGSize(width: frame.width, height: .greatestFiniteMagnitude)) ?? .zero let desiredButtonSize = CGSize(width: labelSize.width + titleEdgeInsets.left + titleEdgeInsets.right, height: labelSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom) return desiredButtonSize } …
You can replace target-action with a closure by adding a helper closure wrapper (ClosureSleeve) and adding it as an associated object to the control so it gets retained. This is a similar solution to the one in n13‘s answer. But I find it simpler and more elegant. The closure is invoked more directly and the …