I have a question about user interface in iOS, especially on iPad.
My app displays restaurant information from a search result. Not all fields are required, so some fields are empty. They could be phone numbers, ratings, menus, and etc.
So basically what I would like to do is to display views such as UILabel and UIButton in a layout format, but I don't want to display any views with empty string. So there should not be any empty space between views.
The way I do is if a field is not empty, I initiate its view and then display below the previously displayed view by keeping track of the current height a view should be displayed.
Although this works, I believe that the way it works seems tedious. Is there the best practice for displaying views with relative positions?
Thank you :)
I've created a library to solve just this problem: CSLinearLayoutView
You use it like this:
// create the linear layout view
CSLinearLayoutView *linearLayoutView = [[[CSLinearLayoutView alloc] initWithFrame:self.view.bounds] autorelease];
linearLayoutView.orientation = CSLinearLayoutViewOrientationVertical;
[self.view addSubview:linearLayoutView];
// create a layout item for the view you want to display and add it to the layout view
CSLinearLayoutItem *item = [CSLinearLayoutItem layoutItemForView:someView];
item.padding = CSLinearLayoutMakePadding(5.0, 10.0, 5.0, 10.0);
item.horizontalAlignment = CSLinearLayoutItemHorizontalAlignmentCenter;
item.fillMode = CSLinearLayoutItemFillModeNormal;
[linearLayoutView addItem:item];
// add more items
A way to do that is to use UITableView and follow what is said in this answer:
Auto adjust the UITableViewCell height depend on its contents in Objective-C
Related
I want to show a logo UIView always on top when the app running,
I know there is a way to do that,add same UIView to every UIViewController,
but I think this is not the best way to do that.
when i have lot of pages,and modify the logo UIView,must modify it every page.
Did someone have better way to do this?
thanks.
look like this:
Since you only every have one window per app, and view's don't have levels, you have to make sure that view stays on top of the hierarchy, no matter what. One relatively easy way is to add it directly to the window above the rest of the interface (the navigation controller):
In applicationDidLaunch:
// After the main navigation controller or tab controller has been added
// either programmatically or in the xib:
UIImage *logo = [UIImage imageNamed:#"logo.png"];
UIImageView *logoView = [[UIImageView alloc] initWithImage:logo];
[self.window addSubview:logoView];
Actually, I think that (a) creating a subclass of UIView that shows your logo and has all the necessary setup in it and then (b) adding this subclass to each view controller is the cleanest and most manageable way to do this.
The reason I prefer this method over adding the view to the window is because if you ever have a view that you don't want to show the logo, you won't need to show and hide something you added to the window. Also, adding directly to the window may cause rotation challenges on certain iOS devices in my experience, depending on what you're doing.
Also, to make sure your logo view is always on top of the view hierarchy, you can do two things:
If the view already exists, you can bring it to front using [UIView bringSubviewToFront:]
[myParentView bringSubviewToFront:myLogoSubview];
If you are creating the view, it will be on top when you add it with [UIView addSubview:]
// Set up myLogoSubview first here with alloc+init, etc.
[myParentView addSubview:myLogoSubview];`
It looks like in your image you would replace myParentView with self.view and myLogoSubview with the view you're looking to keep on top, but this is just my assumption based on your image.
Im building an iPad app which has a tableview embeded within a UIview (odd, i know). In the UITableview there is data loaded in via a PLIST. I want to allow a user to "select" a row and it would show a "popover" (uiview) containing a textfield allowing them to edit that field.
However, looking through the code i see there are only two ways to fire the function:
presentPopoverFromBarButtonItem
presentPopoverFromRect
IS this true, can you only fire that function from either a round rect or a bar button item? I have a feeling i can be done, however searching has not turned up any answers.
Yes you can only present a popover from a bar button item or from a rect in a view.
From the official doc:
Presenting and Dismissing the Popover
– presentPopoverFromRect:inView:permittedArrowDirections:animated:
– presentPopoverFromBarButtonItem:permittedArrowDirections:animated:
– dismissPopoverAnimated:
Those are the only two ways to present a subclass of UIPopoverController. It is not clear in your question as to why "presentPopoverFromRect" wouldn't work.
I understand that "presentPopoverFromBarButtonItem" is a bit limiting but "presentPopoverFromRect" lets you do whatever you want. It allows you to position a popover relative to a view where the "rect" provides a way to offset the popover relative to the view's frame.
Try:
CGRect popoverRectFromCell = CGRectMake(tappedCell.frame.size.width / 2, tappedCell.frame.size.height / 2, 1, 1);
[cellPopover presentPopoverFromRect:popoverRectFromCell
inView:tappedCell
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
Of course you can tweak the "popoverRectFromCell" to achieve desired results.
However, I am not sure this is really a use-case for a UIPopoverController. Might be easier to just make a custom view and use that for text input when the user taps a cell.
Two questions:
I am trying to build a app that has a table view. I would like the user to click on a topic and then the next table view has a image on the left and text on the right. Does anyone know any sample code that would help me accomplish this?
Example:
iPad --> WiFi, and WiFi + 3G
iPhone --> AT&T, Verizon
Also is their a way to tag images in xCode? So in my table view I can only pull images with tag "x"?
Check out http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html for sample code on how to implement table views.
I don't understand your question about tagging. You can use [UIImage imageNamed:] to pull in images that are bundled in your app. Name them as you will.
See Dispelling the UIImage imageNamed: FUD for a discussion of this method.
First of all make a navigation based app. This gives you an app with a view controller in place with a table view on the screen.
Take a look at these for more info:
UITableViewController Class Reference
UITableViewCell Class Reference
In this last one take a look at initWithStyle:reuseIdentifier:, this will allow you to use the style you need, if you have default and add a picture to this cell it will show in the left side, pushing the text a little to the right.
For tagging an image you can simply do
image.tag = tagNumber;
For the cell image you can have like this:
image.tag = tagNumber;
cell.imageView.image = image;
I'm working on an an app that updates both a UITableView and a UIScrollView. I like how UITableView updating works. As I add, remove, or update items, I call insertRowsAtIndexPaths:withRowAnimation:, reloadRowsAtIndexPaths:withRowAnimation:, and deleteRowsAtIndexPaths:withRowAnimation: as appropriate. However, updating the UIScrollView has turned out to be more of a challenge.
So how can I do it? My UIScrollView contains images, and I want to be able to insert, update, and remove individual images -- with animation -- efficiently and smoothly, and perhaps in random order. How does one do this sort of thing?
As an example, I have code like this in my UIScrollViewDelegate implementation for adding a new image:
if (newImageindex == currentImageIndex) {
[scrollView setNeedsDisplay];
[scrollView setContentOffset:portalView.contentOffset animated:YES];
} else if (newImageIndex < currentImageIndex) {
currentImageIndex++;
CGPoint offset = portalView.contentOffset;
offset.x += portalView.frame.size.width;
portalView.contentOffset = offset;
}
This is close, I think, but not quite right. I end up with the images added, but the UIScrollView seems to scroll to a position before the first image in the view. If I start scrolling it by hand, the first image appears. It's like it's scrolled to position -1 in my images.
The example may not help to highlight my problem much, but surely it's a common need to dynamically rejigger the images appearing in a UIScrollView. What's the state of the art on this?
A UIScrollView is not so specialized and structured as a UITableView. The former is a generic view that scrolls and zooms anything you put in it, and you can put any subviews into it anywhere, whereas the latter is made especially to display lists of cells stacked on top of each other.
So the answer is: you need to animate the subviews in the scrollview yourself. There are various libraries like Three20 which provide frameworks for creating more advanced views, you'll need to use a suitable component from one of those or roll your own.
I am wanting something similar to how iWork has the template selection screen for Pages when you can select different templates, and each view contains different info has difference sizes etc.
I have tried subclassing NSCollectionView and determining which view to display using the newItemForRepresentedObject method (as opposed to using itemPrototype view Interface Builder), but it for some reason doesn't position the views correctly, and it does not show the correct number of views for the number of items present. Here is my code. I was hoping someone may have a better way to do this, or an example of how this is done.
personView and companyView are properties in the subclassed NSCollectionView, that are IBOutlets to views in IB.
-(NSCollectionViewItem *)newItemForRepresentedObject:(id)object{
NSCollectionViewItem *collectionViewItem = [[NSCollectionViewItem alloc] init];
[collectionViewItem setRepresentedObject:object];
if([[object valueForKey:#"company"] boolValue] == YES){
NSView *view = [companyView retain];
[collectionViewItem setView:companyView];
}else{
[collectionViewItem setView:personalView];
}
return collectionViewItem;
}
(It doesn't even seem possible to make an NSCollectionView with differently-sized item views; each size would need to be a multiple or integer divisor of some "main" size, and you'd need to do massive item-checking and -reordering to be sure it's even possible to render them in a grid. Are you sure you're asking the right question?)
Also, I don't see anything like this in iWork: all the views in its template chooser are the same. (Though their NSImageView subviews are of different sizes.) I'd recommend if at all possible using the same view and changing its subviews appropriately. It's easy to, for example, bind text fields' "hidden" property or change the width of an image view. Can't you make a single view that works for both classes, changing itself appropriately depending on the represented object?