How do i create Uistackview and add it into a view? - iphone

#Ali
how do i create a uistackview and use into my view? ive got an image showing what i mean
Image - UIstackview

let stackView = UIStackView()
addSubview(view: stackView)
addConstraint(NSLayoutConstraint(...probably constrain all 4 sides or however you want to here.

Related

Set NSButton size in NSStackView

I programmatically create buttons using this code
let button: NSButton = NSButton()
button.action = #selector(ViewController.eject)
button.bezelStyle = .texturedSquare
buttons.append(button)
stackView.addView(buttons[i], in: .leading)
The problem is that those buttons appear like that
I would like to set the (x , y) position and the size of those buttons. For example, I would like to set their width to fill the whole space.
I tried multiple solutions:
button.frame = CGRect(x: 0.0,y: 0.0,width: 100.0,height: 100.0)
and play with the arguments, but still their size don't change.
I then tried to add them as SubViews to stackView, but obviously they overlap with each other.
Any suggestion?
To fix those buttons you first have to modify the StackView element in the storyboard.
Go to the menu of the storyboard and modify it like that
With the "fill" distribution you let the buttons to fill the entire width of the stackview.
With "spacing" you define how much space there has to be between each button.
Then you go there
To give some space to the buttons just play with the Edge Insets values.
Then set the horizontal Hugging Propriety to 250.
The last thing you have to do is to add this code in your ViewController file.
button.setContentHuggingPriority(NSLayoutConstraint.Priority.init(1), for: .horizontal)
button.translatesAutoresizingMaskIntoConstraints = false
In this way you'll the the horizontal Hugging Propriety of the button to 1.
Here's the buttons I created

How I can show/hide view inside another view with height changing of root swift?

I have two views one inside another, it looks like picture below:
when I press on orange arrow I would like to show/hide view above grey line and grey line too. I did it by such way:
#objc func showHide(tapGestureRecognizer: UITapGestureRecognizer)
{
let tappedImage = tapGestureRecognizer.view as! UIImageView
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.jobDataView.isHidden = !self.jobDataView.isHidden
self.view.layoutIfNeeded()
})
tappedImage.image = self.jobDataView.isHidden ? UIImage(systemName: "arrow.down"):UIImage(systemName: "arrow.up")
}
and my view above gray line can be hidden and shown. But root view doesn't change its' height and it has similar sizes before and after btn click. I tried to add constraint of height, but I it didn't solve my problem. Maybe someone knows how to solve my problem?
You need to use UIStackView either through Storyboard or from code. when you hide subview inside stackview it will automatically change stackview height. what you need to do is to make stackView distribution = fill and use vertical stack...
Hide view on button tap and when you need to show it .. add it in stack at. 0th index ..

How can I place my container view under my stack view from another class?

I made a breadcrumb navigation and placed some items in it to show where the user is at the moment.
In my ProjectsOverViewController class is a IBOutlet with a stackView with some buttons. Outlet for my stack view is in another class and I canĀ“t access it from my BreadCrumbNavigation.
In my BreadCrumbNavigation is a container with some labels and controls. The container is placed randomly in my view. But I want it right under my stackView.
let container = UIView(frame: CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height + self.navigationBar.frame.height, width: self.view.bounds.width, height: self.config.height))
Is there a way to grab the stackView outlet and place my container right under it?
use constraints, try to get the bottom anchor for the stackView and alight your container's topAnchor to the stackView's bottomAnchor,
heres an example similar to your need, hope it helps
Align view to bottom in UIStackView

StackView has stopped getting a height in Xcode 10 / iOS 12

Im creating a stackView through my code and adding it to another stackView which is inside a scrollView. After Xcode 10 / ios 12 update my stack view is somehow taking 0 height due to which my scrollView is not able to get a contentSize.
My Code is:-
fileprivate func createStackViewForRowWithOptionLabel(_ label: TiVoLabel, andControlContainer: UIView) -> UIStackView
{
let itemStackRow = UIStackView()
itemStackRow.translatesAutoresizingMaskIntoConstraints = false
itemStackRow.axis = .horizontal
itemStackRow.alignment = .fill
itemStackRow.distribution = .fill
itemStackRow.spacing = 5
itemStackRow.setTheme(.default)
itemStackRow.addArrangedSubview(label)
itemStackRow.addArrangedSubview(andControlContainer)
NSLayoutConstraint.activate([itemStackRow.heightAnchor.constraint(equalToConstant: 30)])
return itemStackRow
}
I've tried doing whatever is mentioned in Here but its not working
I added a height constraint to the stackView equal to the view in which the scrollView is. Reduced its priority to 750. This seemed to have fixed the issue for me. Haven't been able to figure out the exact reason why in iOS 12 its required to explicitly specify the height.

Swift stackview add subview in center

I use this code for view nib to stackview
for index in 0..<4 {
let view = CategoryClass.createMyClassView()
view.myLabel.text = "Hello World!"
self.stackView.addArrangedSubview(view)
}
And I get below image :
But I want to add subview by category.xib height ( 40px per view ) not fill.
And set in center of parent
Like below :
Screenshot:
Please add Four label in Xib and after put in stackView and give spacing like 5, 10, 20 whatever you like
Please check screenshot and let me know if u have any problem related stackview i will help you
you need to delete the top and bottom constraints of the stackView. Or you can delete any one and make the other constraint in greater than or equal to form
This will solve your problem