Trouble with Swift, Auto Layout, and stack views - swift

I’ve been working on an app with a grid of buttons. The grid consists of 4 buttons per row, and (currently) 6 rows. In the storyboard, each row of buttons is in a horizontal stack view, and all 6 stack views are in a vertical stack view.
I don’t want all of the buttons to be visible all the time, so I’m turning them on and off with .isHidden. This is causing some problems when I run the app in the simulator:
I want all of the buttons to stay the same size, but if one or more buttons in a given row / stack view are hidden, the remaining buttons in that row adjust their sizes to fill the row. I’m guessing that a combination of constraints on the buttons and settings on the stack view will solve this, but I haven’t come up with the right combination yet.
If I start with, say, the first 3 rows of buttons all unhidden, then try to unhide a button in one of the other rows, all of the buttons disappear. However, if I ‘print’ the .isHidden state of each button, the ones that should be visible have .isHidden = false.
Any ideas for solving either of these problems?
Thanks in advance for any assistance.

To answer your first question...
When you hide a view in a UIStackView, auto-layout treats it as if it is "gone" --- the stack view will re-layout its arrangedSubviews as if that view was never there to begin with.
If you want this result:
Your best bet is probably to set the .alpha property of that view / button to 0. It will be completely invisible (so, in effect, "hidden") and the stack view will retain its current layout. And, controls with .alpha = 0 do not respond to touches (so you can't tap the invisible button).
For your second question, I'd recommend you re-post that question on its own. Make sure to include the code you're using that is not having the desired result.
See: How to Ask

Related

Why Xcode Storyboard preview and emulator button texts are not compatible?

I am trying to drag and drop buttons to the storyboard. The buttons seem good in my storyboard and preview like the following;
However, when I run the simulator, the button texts seem like the following;
Why the buttons are not shown to fit in the buttons of the simulator and how can I fix this?
Note: Preview and device types are iPhone13 Pro.
The iOS 15 / Xcode 13 style buttons are highly dependent on auto-layout.
Based on your screen-shots, it doesn't look like you've given the buttons any constraints.
You do NOT need to set widths or heights, but you DO want to set at least horizontal and vertical position constraints.
So, constrain all 4 of your buttons centered horizontally, and constrain:
First button Top to view Top (safe area)
Second button Top to First button Bottom
Third button Top to Second button Bottom
Fourth button Top to Third button Bottom
Then you should see them laid-out correctly at run-time.
I think it's because of the auto layout constraints. I am not very familiar with storyboards, if you don't set width of the view component, it seems fine on the storyboard but when compiling the view it actually has default size.
Try to set some constraints for width. Maybe it would help.
The first thing you need to do is to create identical buttons with identical size and with identical font size.
As you can see in your project, the buttons have different sizes, but the text is the same size in all buttons.
To make it faster - you can create one button and make a copy with option + drag’and’drop…
Then, you can put them in a Stack View. So, it will be easier for you to work with them in the future.
Select all buttons and make a Stack View...
https://i.stack.imgur.com/QLTJP.png
https://i.stack.imgur.com/OlOia.png
After that, resize your Stack View like you want.
Then, tap on a Stack View and clear the constraints.
https://i.stack.imgur.com/1pMT8.png
Fix the dimensions like this. But, without “Constrain to mergins”.
https://i.stack.imgur.com/8HKKF
After that, make for the Stack View - horizontally and vertically position in your storyboard.
https://i.stack.imgur.com/a29wL.png
The result is…
https://i.stack.imgur.com/mvQjg.png
Hope it’s break your problem! :)

UIStackView resizing my views

Im making a custom keyboard, and wanting a label and a textField centered in the space left over the keyboard, I figured the proper way would be to incorporate them in a UIstackView, and then centering the stack. However, i'm having som issues with the stack resizing my textField, causing whatever text i enter to be clipped.
I tried adding compressionresistance and a number of different solutions, but its clear to me that im missing some information about how UIStackViews work. Usually i make them work, but the whole resizing part I dont understand.
The first two screens are without a stack, adding them as subview to the viewcontrollers view, and the second screenshot shows the stackView resizing my textfield everytime the keyboard is resigned.
How can i stop the stackview from resizing its contained views?
It seems UIStackView is inherently a auto layout component, and when it adds your arranged subviews, it automatically positions them using constraints.
Since the UIStackView is adding constraints of its own, and the best way I found to stop it, was to simply explicitly set the subviews constraits:
textField.widthAnchor.constraint(equalToConstant: 300).isActive = true
Apple doesnt like dynamic StackViews: "In general, you should not use dynamic stack views to simply implement a scratch-built table view clone." Read the
Apple Documentation on UIStackView for details.

iOS - UITableView Pan Gestures

This may turn out to be 2 questions in one, but I believe solving the first will go most of the way to solving the second.
First, I have created a view in Interface Builder (I know!) and I load the xib file into my view controller in the app delegate. All of this runs smoothly and as expected.
The view consists of 3 table views, two of a similar size and one small one in a corner.
A cell is added to one table view a pan gesture is added to allow movement around the scene.
However, if a cell is moved from it's table view, it appears to go BEHIND the parent view, as per the screenshot below:
The grey line between the two tables is the gray background of the parent view. If the user still has his/her mitts on the cell, they can drag it into view but how can I make it so that all table views are on the same 'layer'?
I.e. so that dragging a cell from one table view to another with show the cell hovering over both views.
This leads me onto my second question, which I will not ask yet as I believe the solution to this will solve my current issue. but I will explain for further clarity.
In my pan gesture, I use a point inside check to see if the cell is within a table view, currently panning a cell from any table makes it print. It is almost like the views take up the whole screen, even though they are sized not to?
All ideas welcome! Thanks!
This was solved by listening for the cell leaving the bounds of the table view, when this happened, I transferred the cell across the table view that it has been dragged to, adding it to the bottom of the list.
Not the most elegant solution and you do lose the touch but it does work,

UIScrollView always bounce upwards and does not show scroll bar in iphone

This is probably simple but I do not seem to get it to work. I have a view and inside it I have a scroll view and inside it I have a view with some labels and a button. the height of the text inside the labels changes according to some condition so I need to scroll down to see it. But whenever I try to scroll down it bounce back up without giving me a chance to view the rest of the view.
Basically, I want when I scroll down, the view to remain down as it normally should. Besides I do not see the scroll bar at all when I'm scrolling.
I know I probably do not understand how scroll views work, so I'd appreciate any help to explain to me the behavior of scroll views.
P.S. I built my whole view in a nib file and this specific setup That I mentioned at the beginning is based on a suggestion from one question I read here.
Thanks, Mohsen
you need to set content size of your scroll view
[scrollView setContentSize:CGSizeMake(360,1000)];
you can make the content size dynamic as per your calculation.

UITableView doesn't scroll when number of items is less than scrollable limit

I've a UITableView and I populate data from a service. When the number of rows is higher than what could be displayed on the screen, I'm able to scroll the table. But when it's less (say 1 or 2), I'm unable to scroll (in the sense, the bouce effect doesn't work making the UI appear odd).
I think this should be fixed by some IB checkbox, but not sure how to do it. Anyone faced this issue before?
The solution that worked for me was to set bounces to YES programmatically in viewDidLoad.
I also have bounces set to YES using the checkbox in IB. It does appear to be taking the IB value because bouncing will work after I've pushed another view controller onto the navigation controller stack and then popped it back off. However, bouncing won't work when the table view first appears unless I set bounces to YES in viewDidLoad.
Click on the table view and look at the Attributes Inspector. There's a checkbox for Bounce Scroll. (I just tested this in a project that had a 2-line table; disabling it prevented scrolling, enabling it allows the short table to bounce)
I think the programatic equivalent is the bounces property.
Do you have this table view as a cell inside another table view?