Unresponsive UIButton in subview added to UIStackView
It seems to be that something is lying over your button and catching all the touch events.
1. Never turn on userInterectionEnabled if you don't need it
There is no reason why a normal view like subView should have set userInteractionEnabled set to true so put it to false.
2. Find the bug:
To find out witch view catches the event start your app on a device and navigate to the stackView. Now in Xcode, press the button "Debug View Hierarchy" (placed right over the console output)
After that you will see every view currently displayed on your device.
What you do now is finding out wich views are above your button, and then in code turn their userInteractionEnabled value to false.
Action not being called when button is tapped in a stack view
You can use Protocol/Delegation
//1. Create a protocol
protocol HomeViewDelegate{
func loginButtonClicked(sender: UIButton)
}
class HomeView: UIView {
//2. Create a delegate
var delegate: HomeViewDelegate?
let stackView: UIStackView = {
let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.distribution = .fillProportionally
stack.alignment = .fill
stack.isUserInteractionEnabled = false
return stack
}()
let haveAccount: UILabel = {
let label = UILabel()
return label
}()
let signin: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Sign in", for: .normal)
button.titleLabel?.font = UIFont(name: "Avenir", size: 14)
button.setTitleColor(UIColor.white, for: .normal)
button.addTarget(self, action: #selector(loginClicked(sender:)), for: .touchUpInside)
button.backgroundColor = .red
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = false
addSubview(stackView)
stackView.addArrangedSubview(haveAccount)
stackView.addArrangedSubview(signin)
stackView.setCustomSpacing(4.0, after: haveAccount)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//3. Call your protocol method via delegate
@objc func loginClicked(sender: UIButton) {
if let delegate = delegate{
delegate.loginButtonClicked(sender: sender)
}
}
}
In You Caller ViewController create an extension
extension ViewController: HomeViewDelegate{
func loginButtonClicked(sender: UIButton) {
print("login Button Clicked")
}
}
UIButton in UIStackView button click is not working
Please check following -
- User interaction of each superview.
- IBAction for all buttons.
If you are still having issue then let me know. Happy coding
Adding subview to uistackview not working on button click
Here is my sample code base on yours:
Notes: I set my stackview with :
Distribution set to "Fill equality" and Alignement set to "Fill"
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
AddViewsInStackView(); // Add views first time in the stackview
AddButtonInStackView(); // Add the "Add button" first time in the stackview
}
private void AddViewsInStackView()
{
// Create 2 views and add them to the stackview
var view1 = new UIView { BackgroundColor = UIColor.Red };
var view2 = new UIView { BackgroundColor = UIColor.Green };
stackView.AddArrangedSubview(view1);
stackView.AddArrangedSubview(view2);
}
private void AddButtonInStackView()
{
// create the "Add button"
var button = new UIButton(UIButtonType.Custom);
button.SetTitle("Add", UIControlState.Normal);
button.SetTitleColor(UIColor.Blue, UIControlState.Normal);
button.TouchUpInside += (sender, e) => AddSupplementaryViews();
stackView.AddArrangedSubview(button); // Add the "Add button" in the stackview
}
private void AddSupplementaryViews()
{
if (stackView.ArrangedSubviews.Any())
{
// Fetch the "Add button" then remove them
var addButton = stackView.ArrangedSubviews.Last();
stackView.RemoveArrangedSubview(addButton); // This remove the view from the stackview
addButton.RemoveFromSuperview(); // This remove the view from the hierarchy
AddViewsInStackView(); // Add new views
AddButtonInStackView(); // Add it again so it will be the last one every time
}
}
Button in UIStackView not clickable
You are adding the view controller as a subview. So you also need to add as a child.
Add bellow code after self.stackView.addArrangedSubview(btn.view)
this line.
self.addChild(btn)
btn.didMove(toParent: self)
button stackView spacing not working -Swift - Programmatically
UIStackView
tries to fill the entire width with its contents. So you have some options here
Center
Since the stackView's content size is based on its content, you should consider getting rid of the leading
and trailing
anchors and use a horizontal center instead. (You can keep one of them alongside with the center one to prevent it from overlapping the edges)
Dummy views
Another option is to add. dummy views at both sides of the stack view (inside), make them have clear
color and let them be the last to hug. So. they. can. fill extra space.
Other options
There are other options (like not using the stack view at all) that you can implement to make it happen.
UIButton targets not working if a UIView is added as a subview
Set userInteractionEnabled
to NO
for your subviews to let touches pass through them and onto your button.
Also make sure to change _verticle.userInteractionEnabled = NO;
in your lazy-loader for your horizontal
property to _horizontal.userInteractionEnabled = NO;
as I believe that's a typo.
Related Topics
Property with '= {Return}()' or '{Return}'
How to Cast from Uint16 to Nsnumber
Why Can't I Use .Reduce() in a One-Liner Swift Closure with a Variadic, Anonymous Argument
Swift - Getting Only Alphanumeric Characters from String
C-Style Uninitialized Pointer Passing in Apple Swift
How to Increase the Scope of Variables in Switch-Case/Loops in Swift
Reading Data from Excel Document in a Swift App
How to Edit Swift Error Breakpoint
Passing Dynamic Int Variable from One Class to Another Class in Swift
Fetching Child Sum from Core Data
Get Current Url from Browser in MACos
Running Swift Build in Terminal Leading to "Platform Path" Errors
Swift: Reflecting Properties of Subclass of Nsmanagedobject
In Swift, Why Can't I Instantiate a Protocol When It Has an Initialiser
How to Get the Url from Webview in Swift