Uisegment buttons over scroll view - uiscrollview

hi iam trying to put a uisegmented controls over a uiscrollview beacuse i want them to scroll horizontally this is the code i tried
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 600, 300)];
scroll.contentSize = CGSizeMake(500, 50);
scroll.showsHorizontalScrollIndicator = YES;
scroll.pagingEnabled = YES;
scroll.showsHorizontalScrollIndicator = YES;
scroll.autoresizingMask = YES;
scroll.autoresizesSubviews = YES;
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three",#"four",#"Five", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0,10, 500, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 4;
[scroll addSubview:segmentedControl];
[self.view addSubview:scroll];
any one please help me to accomplish this . thanks in advance happy coding

The frame for the scroll View must be less than the Content size of scroll view. If you will increase your content size definitely it will work. For example:-
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
scroll.contentSize = CGSizeMake(500, 50);
scroll.showsHorizontalScrollIndicator = YES;
scroll.pagingEnabled = YES;
scroll.showsHorizontalScrollIndicator = YES;
scroll.autoresizingMask = YES;
scroll.autoresizesSubviews = YES;
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three",#"four",#"Five", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0,10, 500, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 4;
[scroll addSubview:segmentedControl];
[self.view addSubview:scroll];
If its supportive please mark the answer correct. Thanks very much.

Related

Adding UISegmentedControl to UIScrollView programmatically

I am tyring to add UISegmentedControl to UIScrolview programmatically.
I am using below code..
I don't why the scrollview is not enabled.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 435)];
scroll.contentSize = CGSizeMake(320, 700);
scroll.showsHorizontalScrollIndicator = YES;
NSArray *menu = [NSArray arrayWithObjects:[UIImage imageNamed:#"Accountimg.png"],[UIImage imageNamed:#"Challengesimg.png"],[UIImage imageNamed:#"Profileimg.png"],[UIImage imageNamed:#"Challengesimg.png"],nil];
UISegmentedControl *menuSegmentedControl = [[UISegmentedControl alloc] initWithItems:menu];
menuSegmentedControl.frame = CGRectMake(0,0,450,40);
menuSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[scroll addSubview:menuSegmentedControl];
[menuScrollView addSubview:scroll];
[menuSegmentedControl release];
[scroll release];
I am unable to scroll horizontally..
Thanks in advance
UPDATE:
I am able to get the horizontal scroll.
Code updated:
menuSegmentedControl.frame = CGRectMake(0,0,500,35);
menuSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
menuScrollView.contentSize = CGSizeMake(menuSegmentedControl.frame.size.width, menuSegmentedControl.frame.size.height);
menuScrollView.showsVerticalScrollIndicator = NO;
[menuScrollView addSubview:menuSegmentedControl];
menuScrollView.clipsToBounds = YES;
Thank you.

How can we put two line in UIBarButtonItem in Navigation Bar

"Now Playing" is in One line in UIBarButtonItem. I have to put it in two lines, like "Now" is ay top and "Playing" is at bottom.I have written the following line of code:-
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Now Playing"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)];
self.navigationItem.rightBarButtonItem = flipButton;
So i want to pu line break in between "Now Playing". So please help me out.
Yes you can. It is fairly simple to do. Create a multiline button and use that. The "trick" is to set the titleLabel numberOfLines property so that it likes multilines.
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.numberOfLines = 0;
[button setTitle:NSLocalizedString(#"Now\nPlaying", nil) forState:UIControlStateNormal];
[button sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
When you specify a button type of custom it expects you to configure everything... selected state, etc. which is not shown here to keep the answer focused to the problem at hand.
- (void)createCustomRightBarButton_
{
UILabel * addCustomLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 25)] autorelease];
addCustomLabel.text =#"Now\nPlaying";
addCustomLabel.textColor = [UIColor whiteColor];
addCustomLabel.font = [UIFont boldSystemFontOfSize:11];
addCustomLabel.numberOfLines = 2;
addCustomLabel.backgroundColor = [UIColor clearColor];
addCustomLabel.textAlignment = UITextAlignmentCenter;
CGSize size = addCustomLabel.bounds.size;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
[addCustomLabel.layer renderInContext: context];
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage * img = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:img
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)]
autorelease];
}
You can host a UIButton as customView inside your bar button (either set the bar button item customView or you can drag one directly on top of your UIBarButtonItem), hook it up as an outlet, then do;
-(void) viewDidLoad
{
//...
self.customButton.titleLabel.numberOfLines = 2;
self.customButton.suggestionsButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.customButton.suggestionsButton.titleLabel.textAlignment = UITextAlignmentCenter;
}
which in my case becomes
I create 2 PNG images by myself, and it looks good.
UIImage *img = [UIImage imageNamed:#"nowplaying.png"];
UIBarButtonItem *nowPlayingButtonItem = [[[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:delegate action:#selector(presentNowPlayingMovie)] autorelease];

How to put programmatically iCarousel in View?

I want four ScrollViews like iCarousel's CoverFlow in View programmatically. My Code is
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//set up data
wrap = YES;
self.items = [NSMutableArray array];
for (int i = 0; i < NUMBER_OF_ITEMS; i++)
{
[items addObject:[NSNumber numberWithInt:i]];
}
carousel1 = [[carousel1 alloc] init];
carousel2 = [[carousel2 alloc] init];
carousel3 = [[carousel3 alloc] init];
carousel4 = [[carousel4 alloc] init];
carousel1.frame = CGRectMake(0, 0, 320, 120);
carousel2.frame = CGRectMake(0, 120, 320, 120);
carousel3.frame = CGRectMake(0, 240, 320, 120);
carousel4.frame = CGRectMake(0, 360, 320, 120);
carousel1.delegate = self;
carousel2.delegate = self;
carousel3.delegate = self;
carousel4.delegate = self;
carousel1.dataSource = self;
carousel2.dataSource = self;
carousel3.dataSource = self;
carousel4.dataSource = self;
[self.view addSubview:carousel1];
[self.view addSubview:carousel2];
[self.view addSubview:carousel3];
[self.view addSubview:carousel4];
//configure carousel
carousel1.type = iCarouselTypeCoverFlow2;
carousel2.type = iCarouselTypeCoverFlow2;
carousel3.type = iCarouselTypeCoverFlow2;
carousel4.type = iCarouselTypeCoverFlow2;
}
but fail with four warnings with out placing the carousels view on the View. Please help me to overcome this problem. Thanks in advance.
Shouldn't you be initialising them with [[iCarousel alloc] init] instead of [[carousel1 alloc] init]?

How to modal the view when network connecting

Is there any way to set the view page modal during network connecting? I mean all controllers in page do not response the user operation if the network is connecting?
I updated code like:
- (void)loadView
{
[super loadView];
_hudView = [[UIView alloc] initWithFrame:CGRectMake(75, 155, 170, 170)];
_hudView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
_hudView.clipsToBounds = YES;
_hudView.layer.cornerRadius = 10.0;
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
_activityIndicatorView.frame = CGRectMake(65, 40, _activityIndicatorView.bounds.size.width, _activityIndicatorView.bounds.size.height);
[_hudView addSubview:_activityIndicatorView];
[_activityIndicatorView startAnimating];
_captionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)];
_captionLabel.backgroundColor = [UIColor clearColor];
_captionLabel.textColor = [UIColor whiteColor];
_captionLabel.adjustsFontSizeToFitWidth = YES;
_captionLabel.textAlignment = UITextAlignmentCenter;
_captionLabel.text = #"Loading...";
[_hudView addSubview:_captionLabel];
NSArray *buttonNames = [NSArray arrayWithObjects:#"hehe",#"xixi",nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
segmentControl.frame = CGRectMake(0, 0, TTApplicationFrame().size.width/2, TT_ROW_HEIGHT-10);
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentControl.momentary = NO;
segmentControl.tintColor = [UIColor darkGrayColor];
segmentControl.selectedSegmentIndex = 0;
[segmentControl addTarget:self action:#selector(switchPage:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentControl;
TT_RELEASE_SAFELY(segmentControl);
self.view = [[[UIView alloc] initWithFrame:TTApplicationFrame()] autorelease];
self.tableView = [[[UITableView alloc] initWithFrame:TTApplicationFrame() style:UITableViewStylePlain] autorelease];
self.tableView.rowHeight = 80.f;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];
[self reload];
[self updateView];
}
and show it like:
- (void)showLoading:(BOOL)show {
if (show) {
self.loadingView = _hudView;
}
else {
self.loadingView = nil;
}
}
As I said in comment, it still cannot cover the buttons of UISegmentedControl, user still can click those buttons...
Instead of using a modalViewController, create a HUD (one of the nice views with a UIActivityIndicators you'll see in the TTCatalogue app) that will block the view and inform the user something is happening while you're making a request.
Just insert them by a [self.view addSubview:theHUDview];.
It's not necessarily good design though, instead, make your network calls asynchronously, in the background.

Xcode: Scrollview with images in scrollview

I'm trying to make a view looking almost like the info-view in appStore, with text on the first half of the view and images on the second half. I've tried to use a scrollview in another scrollview. The first view (containing a textview on the top half and a scrollview on the bottom half) scrolls nice, but the scrollview at the bottom (containing several imageviews) doesn't scroll.
Any ideas how to make the second scrollview to scroll?
scrollView is the view containing a textview and a scrollview.
imageScrollView is the view with the imageviews.
[scrollView setContentSize:CGSizeMake(320, 585)];
[imageScrollView setContentSize:CGSizeMake(1520, 400)];
I recently figured it out myself, but forgot to post the answer. The problem was that I set the sizes of the scrollviews in .xib to the same sizes as in the code. When I changed it to 320,416 and 320,400 I was able to scroll them with the setContentSize. In the imageScrollView I then added images along the x-axis (first image at 0, second at image width + a small space and so on). Hope this could help someone.
HERE IS CODE OF scroll-view display images from Documents Directory ByType .png Images who seved by user from Iphone Photo Gallery
- (void)viewDidLoad {
int scrollviewwidh = 120;
self.view.backgroundColor = [UIColor darkTextColor];
hiImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 240)];
hiImage.backgroundColor = [UIColor blackColor];
hiImage.userInteractionEnabled = YES;
[hiImage.layer setBorderColor:[UIColor blackColor].CGColor];
[hiImage.layer setBorderWidth:2.01f];
[self.view addSubview:hiImage];
[hiImage release];
scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 260, 320, 110)];
[self.view addSubview:scroll];
scroll.showsHorizontalScrollIndicator = NO;
scroll.pagingEnabled=YES;
scroll.delegate=self;
scroll.contentSize=CGSizeMake(scrollviewwidh,80);
scroll.showsVerticalScrollIndicator =YES;
scroll.backgroundColor = [UIColor darkTextColor];
}
-(void) viewWillAppear:(BOOL)animated{
self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
self.documentsDir = [paths objectAtIndex:0];
NSLog(#"the ----------- %#",documentsDir);
for (UIButton * img in self.scroll.subviews) {
[img removeFromSuperview];
img = nil;
}
int xoffcet = 0;
int col = 0;
int scrollviewwidh = 120;
for (NSString* fileName in [self.fileManager contentsOfDirectoryAtPath: self.documentsDir error:nil]){
if ( [fileName rangeOfString:#".png"].location != NSNotFound ) {
NSLog(#"add %#", fileName);
UIImage* img = [UIImage imageWithContentsOfFile:
[self.documentsDir stringByAppendingPathComponent:fileName]
];
imgView = [[UIButton alloc] init];
[imgView setImage:img forState:UIControlStateNormal];
[imgView addTarget:self action:#selector(imageViewClicked:) forControlEvents:UIControlEventTouchUpInside];
[imgView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[imgView.layer setBorderWidth:1.0f];
imgView.bounds = CGRectMake(10,10, 50, 50);
imgView.frame = CGRectMake(5+xoffcet, 0, 160, 110);
scroll.contentSize =CGSizeMake(scrollviewwidh+xoffcet,110);
[scroll addSubview:imgView];
[imgView release];
xoffcet +=170;
}
if (col++>1) {
//row++;
col = 0;
}
}
self.title = #"saved Photo";
// [scroll release];
[super viewDidLoad];
// scroll.pagingEnabled = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:
UIBarButtonSystemItemCancel
target:self
action:#selector(photogallary:)];
}
-(IBAction)imageViewClicked:(UIButton *)sender{
for (NSString* fileName in [self.fileManager contentsOfDirectoryAtPath: self.documentsDir error:nil]){
if ( [fileName rangeOfString:#".png"].location != NSNotFound ) {
NSLog(#"add %#", fileName);
UIImage* img = [UIImage imageWithContentsOfFile:
[self.documentsDir stringByAppendingPathComponent:fileName]];
hiImage.image = img;
}
}
}
All The Very Best i Hope its useful Thank You If Thats code Help please give Your Feedback

Resources