Uibutton contraints in two layouts - ios

I’m still trying to get my head around constraints and the objective c best practises in general.
I need to dynamically create between 1 to 6 UIButtons depending on a variable. I’m ok with this part but confused with how to position them given that:
The number of buttons could be anything from 1-6.
The buttons have to be of equal size.
Landscape and Portrait orientations will have different layouts.
They have to ‘float’ left in the landscape view and vertically align to the top in portrait view.
I'm guessing I'll need one container that sits at the bottom of the view and changes height depending on the orientation, then have rules for the buttons inside depending on the container width. But how to go about it I have no idea.

In the long run it will be better to use the UICollectionView. Subclass UICollectionView and add it in your Storyboard. Make an outlet of the collection view to your view controller and set its delegate and data source. Add the <UICollectionViewDelegate> and <UICollectionViewDataSource> to your view controller and implement the delegate methods. For the buttons, make a custom UICollectionViewCell with a UIButton as its subview. Then in cell:ForRowAtIndexpath: method, initialize the UICollectionViewCell subclass, and set the properties of the UIButton. This might not be the answer you wanted, but UICollectionView offeres much more flexibility as far as layouts are concerned. You can read more about UICollectionViewDelegateFlowLayout here.

Related

Width and height not adjusting with Xcode Auto Layout

I have created an interface for my app in Storyboard under the Any x Any layout setup, when I run my app the layout is still sized for the square Any x Any and not the phone screen. I want the app to adjust to all sizes and not use a set layout for set screen sizes. How can I do this?
Here are pictures to show my design, and the outcome of running the app.
Storyboard View
Simulator View
The table view is also cut off here
If your ViewController is subclass of UIViewController then add leading, trailing ,top and bottom constraints to UITableView, and contains to your custom navigation bar. or If its subclass of UITableViewController. No need to add any constraints. Just embed your TableViewController into NavigationController.
Hope this will help you
what exactly are you trying to achieve? Your tableview to be entirely on the screen, so not scrollable?
If so, then you should override the layout of your tableview and its corresponding cells. As those cells (and the number of them) define the height of your tableview.
From what I can see it appears that you have manually added a UINavigationBar. I personally find it much easier to embed your view controller into a navigation controller. This will provide you with a navigation bar that will automatically resize depending on the hardware, or size class, of the device. To embed it go to: Editor -> Embed in -> Navigation Controller.
Your TableView seems to be fully in the screen as well? If you have layout issues with your cells not adjusting then I will be more than happy to help you once you clarify what is not quite right.
Hope this helps.
Yep. Been there, done that. Just because something is centered in the Any x Any view in interface builder does not mean it will also be centered e.g. in an iPhone simulator. You always need to make your layout choices explicit with constraints, i.e. in the this case add a "center horizontally in superview" constraint or align the right and left edge with the superview (I really would need to know more about your view controller in interface builder to tell you exactly).
Are you familiar with constraints or should I add some more detail on how to solve this?

Two UIViews & one UICollectionView inside UIScrollView (or better approach)

I need to have iOS app with screen like this:
screen
The idea is when user start to scroll down the first UIView to move up until the second UIView reach the top where it will stick and only UICollectionView will continue to move up.
Currently I'm using this structure
UIScrollView (main scroll)
UIView (some banners)
UIView (UISegmentedControl)
UICollectionView (grid with items, scroll is disabled, main scroll is used)
I manage to do it, but I needed to set UICollectionView height constraint manually in the code (calculated based on all items in grid) in order to appear in UIScrollView (everything else is handled by AutoLayout in Storyboard). The problem with this is that UICollectionView think all cells are visible and load them, so the whole recycling & reusing thing does not work. It's even worst because I use willDisplayCell method on UICollectionView to load more data when last cell is displayed, but now it load all pages at once.
My question (actually they are 2)
How can I fix the issue above?
What is the right way to achieve this functionality? Maybe my whole approach is conceptually wrong?
Collection view is a scroll view itself. So maybe you could have same functionality only with Collection view with sections or even custom layout?

Anchor a view at the bottom of a UITableViewController

I'm building a view which is a UITableView, however I want to anchor a "Write a comment" UITextField at the bottom of the screen. You can see an example of this on these screenshots from the "Secret" app - the "Write a comment (anonymously)" and "Post" button are anchored to the bottom of the screen regardless of whether you scroll the tableview items up or down.
What's the best way to achieve this?
Should I be embedding a UITableViewController into a UIViewController with the UIView anchored to the bottom of the screen and the tableview anchored to the top of this view?
Is this some kind of UITableView section footer?
Any advice greatly appreciated.
You could subclass UITableView and add a bottom view (not to confuse with a tableview's footerView). Since a UITableView is a subclass of UIScrollView, you can change its contentInsets so that the content of the tableview will still scroll above your bottom view.
tableView.contentInsets = UIEdgeInsetsMake(0, 0, 0, bottomViewHeight);
The next step would be to make the bottom view sticky, that is, floating with the bottom of the tableview. You can achieve this in multiple ways. Here are two suggestions:
1) Manipulating the frame directly
By conforming to UITableViewDelegate you automatically conform to UIScrollViewDelegate. You can see this by inspecting the protocol declaration in UITableView.h:
#protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
Then implement scrollViewDidScroll:(UIScrollView *)scrollView and change the y-offset of the bottomview to always position it at the bottom of the view. UIScrollView's contentOffset property is used to determine how far down the scrollview has scrolled. This method will be called every single time the scrollview scrolls, hence it will appear that the bottom view sticks to the bottom of the tableview.
2) Use auto layout
While still changing the contentInsets as above, you can achieve the sticky effect by using auto layout constraints instead. By pinning the bottom view to the edge of the scrollview, it will automatically create the sticky effect for you. This is by far my recommended approach, since it saves lines of code, while it uses the highest possible level of abstraction.
I use this category by Florian Kugler when implementing auto layout in code.
This technical note, however not strictly related to the issue, describes how to use auto layout with scrollviews.
I have in-app chat (like whatsapp) and I have the following structure:
UIViewController
\
- View
\
- UITableView
- UIView (with textfied)
I thinks this is the best approach as you don't mix table data with anything else, and you don't have to juggle with sections in code
if you requirement is to show only one picture related information at once in screen means please follow the process it will help you.
first take a view controller then take an image view and on it the buttons you needed add them on image view only below image view take a tableview(if there are comments show tableview. if no comments available show a label as"be first to comment").
Hope this will help

UICollectionView as subview of UIViewController

I want to use a collection view thats horizontally scrollable along the bottom 25% of my screen full of pictures and then use the top 75% of the screen to display information about the selected cell (zoomed in picture, caption, people in it, etc.) of the collection view. My question is how to go about implementing view controllers in this situation.
Will I need a UIViewController to handle the whole screen and UICollectionViewController to handle the collection view (how do I get them to work together?) Or could I implement the whole screen with just one view controller? (if so, which one?) Should I use a custom CollectionViewController with one section being my main info, and another section being the bottom scrollable part?
No, just have one UIViewController and add the UICollectionView as a subview. Then set the dataSource and delegate and tell it to reloadData. If it seems too easy, you're overthinking it.

Is it possible set width of UITableView or cells on iPad

Since it's not possible to "hook onto" this and get an answer, and none of the answers do it, I thought I'd ask if anyone ever succeeded in doing it and if so, how.
I've created a New File, selected subclass UITableViewController with XIB, and everything works fine. I can set the height of everything (with delegate methods), but not the frame of anything.
Right now the table is as wide as the iPad and it looks kinda corny with the short titles in each cell - and there is nothing in IB that changes the stretching or layout.
I'd be happy with any solution that causes the cells to shrink horizontally - like margins. I tried scrollview insets in IB, but they only add to the size of the scrollview so it gets bigger than the screen.
I think that you want to subclass UViewController and add a UITableView to it, and make it the size you want in IB.

Resources