Domanda

I am new to iOS programming, so I hope its not a stupid question to ask. I have to add ten different buttons in my view programmatically. I know how to add buttons programmatically, but my buttons won't fit within the view so some of them have to be present below the viewable height of the view, so I want to add the buttons such that the user can slide upward and downward to move across the view. How can this be done?

È stato utile?

Soluzione

There are a number of standard UI elements UIKit provides that help handle displaying more content than the screen can display at any one time:

You may benefit from some of the automatic layout and formatting provided by the UICollectionView but from your description it sounds as though either the UIScrollView or UITableView would be best suited for your scenario.

UIScrollView

The UIScrollView acts as the user's viewport into the underlying view it contains and provides complex panning and zooming functionality by default. It is highly configurable and you can prevent zooming functionality if you do not need it.

Just make sure to place your view inside the UIScrollView within Interface Builder and set the contentSize property. Setting a contentSize larger than the bounds of UIScrollView should enable scrolling automatically. To enable zooming have your UIViewController implement the UIScrollViewDelegate:

@interface MYViewController : UIViewController <UIScrollViewDelegate>
@end

Then ensure to return your view in the viewForZoomingInScrollView: method:

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return theViewContainingTheButtons;
}

UITableView

The UITableView is specifically designed to display a list of items that can be vertically scrolled if its content is taller than the size of the view.

A UITableView is composed of a number of UITableViewCells, each of which could contain one of the buttons you wish to display. Apple's Table View Programming Guide for iOS covers most aspects of the UITableView.

Conclusion

The UITableView approach focuses specifically on what you have asked and neglects that there may be other content in the view hosting the buttons. It also means that you would have to change your existing dynamic button placement code and write new code to work with table views. In reality it's likely that the UIScrollView is what your looking for but I wanted to provide exposure to other UI elements you may not have been aware of that could be used to achieve what you have described.

Good luck!

Altri suggerimenti

You need t use a special element, called a UIScrollView. It will handle all this automatically. Here's a link to the docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html

You can use the UIScrollView.

Instead of adding UIButton to UIView addd then to UIScrollView and set UIScrollView contentSize.

Fore more help : UIScrollView from Apple.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top