Segmented Control title shows on simulator but not device - ios

Here's what I see on the simulator:
Here's what I see on the device:
This is the code:
- (void)buildNavBarTitle
{
self.navigationItem.title = nil;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, self.navigationController.navigationBar.frame.size.height)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 160, view.frame.size.height - 10)];
segmentedControl.tintColor = [UIColor blackColor];
[segmentedControl insertSegmentWithTitle:#"New" atIndex:0 animated:NO];
[segmentedControl insertSegmentWithTitle:#"Today" atIndex:1 animated:NO];
[view addSubview:segmentedControl];
self.navigationItem.titleView = view;
}
I have
deleted app on simulator & device
restarted xCode
cmd+shift+k to clean
cmd+shift+alt+k to wipe build folder

I ran into a similar issue when I used UIAppearance to style all controls of that type. It had to do with when I was trying to remove the shadow from the text, it cause the text not to appear. After allowing the shadow, you couldn't notice that it was really there, and the text reappeared.
I have submitted a bug to apple regarding this.

Try this:
NSArray *itemArray = [NSArray arrayWithObjects: #"New", #"Today", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 0;
[view addSubview:segmentedControl];

Related

UIview.hidden doesn't disappear but hides behind another view

So I am trying to hide a UIView but something weird is happening. After using this line of code:
ntcCircleView.hidden = YES;
The view won't disappear, but it hides behind another UIView.
This is the complete code I use:
UIView* NtcContainer=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 40-17, 3, 40, 40)];
UIView* NtcView=[[UIView alloc]initWithFrame:CGRectMake(5, 0, 40, 40)];
notificationButton = [ZFRippleButton buttonWithType:UIButtonTypeCustom];
notificationButton.frame = CGRectMake(0, 0, 40, 40);
notificationButton.layer.cornerRadius=menuButton.frame.size.width/2;
[notificationButton addTarget:self action:#selector(goToNotificationsList:) forControlEvents:UIControlEventTouchUpInside];
notificationImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 12, 20, 20)];
notificationImage.image=[UIImage imageNamed:[HotelStay sharedInstance].icon.Notification];
ntcCircleView = [[UIView alloc] initWithFrame:CGRectMake(20,5,16,16)];
ntcCircleView.alpha = 0.7;
ntcCircleView.layer.cornerRadius = ntcCircleView.frame.size.width/2; // half the width/height
ntcCircleView.backgroundColor = [UIColor redColor];
ntcNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,16,16)];
ntcNumberLabel.textAlignment = NSTextAlignmentCenter;
[ntcNumberLabel setTextColor:[UIColor whiteColor]];
[ntcNumberLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:11.0]];
[ntcCircleView addSubview:ntcNumberLabel];
int ntcNum = [dataManager getUnreadNotificationNumber];
if (ntcNum==0)
{
ntcCircleView.hidden = YES;
}else
{
ntcNumberLabel.text = [NSString stringWithFormat:#"%i",ntcNum];
}
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
[self.view addSubview:NtcContainer];
So ntcCircleView after hidding it, just goes behind notificationImage.
What drives me crazy, is that I use successfully the exact same code in another Views with the only difference the last line. Instead of using:
[self.view addSubview:NtcContainer];
I add the views to the navigation bar like this:
UIBarButtonItem *ntcBarItem = [[UIBarButtonItem alloc] initWithCustomView:NtcContainer];
self.navigationItem.rightBarButtonItem = ntcBarItem;
What am I missing here ?
UPDATE
I also noticed that this bug happens only when I use
[self.navigationController popViewControllerAnimated:YES];
to navigate back to the view.
You need to update these snippet:
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
to:
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcView addSubview:notificationImage];
[NtcContainer addSubview:NtcView];
It will solve your problem.
I figured it out. I was using a deprecated method (viewDidUnload) to release the notification observer.

UISegmentedControl setFrame not working

I am trying to add segmented control to tableviewheader. I am using the following code to do that
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[self.view addSubview:segmentedControl];
segmentedControl.selectedSegmentIndex = 0;
self.tableView.tableHeaderView = segmentedControl;
I can't set the frame, no matter what value is put its always 100% wide. How can i set the add 24px margin on left and right ?
That is because you are using the segment control as a tableHeaderView. a UITableViewHeaderView will be always be as wide as your tableView. You can only change the height.
Adding to akshaynhegde answer, you can easily achieve this by adding an extra UIView as parent and add the UISegmentControl to that UIView. In this case the parent UIView will take the whole width of UITableView but not the UISegmentControl.
So following should be the change in your code
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0,320.0,30.0)];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;
It should solve your problem.
Cheers.
First, you are adding segmentedControl on controller view [self.view addSubview:segmentedControl] and then assigning it to table header view
self.tableView.tableHeaderView = segmentedControl;
Which is wrong, you need to create a view with size table header size and then add segmentedControl add on view and assign this view to table header view.
Example
**UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;**

Setting selected item in UISegmentControl is not working

Trying to set the selected side of a UISegmentControl, but nothing happens.
In my viewDidLoad or viewDidAppear this code will not select a segment?
[self.segmentedControl setSelectedSegmentIndex:[#1 integerValue]];
Nothing happens?
I deleted the UISegmentControl from the Storyboard and added it programmatically and it works now. Very weird.
self.segControl.frame = CGRectMake(35, 100, 200, 50);
self.segControl.segmentedControlStyle = UISegmentedControlStylePlain;
self.segControl.selectedSegmentIndex = 1;
[self.view addSubview:self.segControl];
You can use code for adding and setting selected tab in UISegmentControl
NSArray *itemArray = [NSArray arrayWithObjects: #"Top Twenty", #"Dow thirty", #"Favorite", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(15, 80, 300, 30);
segmentedControl.backgroundColor = [UIColor groupTableViewBackgroundColor];
[segmentedControl addTarget:self action:#selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0; //**this line represent selected tab**
[self.view addSubview:segmentedControl];

Buttons are not seen on the view

I have added buttons programmatically to my view, but only toolbar is visible on the view when i test on the simulator.
What am i doing wrong?
I have defined my ui elements in header file, and synthesized in a main file and then initialized in the viewDidLoad method.
But there is something wrong.
My code is like below;
#import "ViewController.h"
#implementation ViewController
#synthesize iAmLost, iAmLooking, about, howToUse,appsFromKodAtolye, exit;
- (void)viewDidLoad
{
[super viewDidLoad];
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];
iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 200.0, 10.0)];
iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 200.0, 10.0) ];
[iAmLooking addTarget:self
action:#selector(iAmLookingScreen)
forControlEvents:UIControlEventTouchUpInside];
about = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200.0, 10.0)];
howToUse = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200.0, 10.0)];
exit = [[UIBarButtonItem alloc] initWithTitle:#"Exit" style:UIBarButtonItemStyleBordered target:self action:#selector(exitApp:)];
NSArray *buttons = [[NSArray alloc]
initWithObjects:exit, nil];
toolbar.items = buttons;
[self.view addSubview:iAmLost];
[self.view addSubview:iAmLooking];
[self.view addSubview:about];
[self.view addSubview:toolbar];
}
You are placing the buttons underneath the toolbar, you add each item at 0,0. As the toolbar is the last item you add to the subview it will appear on top of all the buttons and therefore you will not be able to see the buttons.
The frame is made up of 4 parts x co-ordinate, y co-ordinate, height, width
Try changing the x and y co-ordinates for these
For example
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];
iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(0,100, 200.0, 10.0)];
iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 200, 200.0, 10.0) ];
You should also note that you currently have not set any style for the buttons or any titles. It might be worth setting titles or a background colour so that you can actually see what buttons you have placed on screen and tell which is which.
All your buttons are below the toolbar since you're adding the toolbar as the last subview.
You have to add your buttons as subviews of the toolbar. Also be aware that you're positioning all the buttons at the same position.
You have total four buttons and one tool bar. The frame of tool bar is totally correct ie
(0, 0, 320.0, 40.0)
But for buttons you are giving same coordinates for all, so it is overlapping. Better you will change button coordinates.
And if you want to add these buttons on tool bar then set coordinate properly and use
[toolBar addSubView: button];
Simple, just check the origin x & y for each Button
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];
UIButton *iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(5,50, 200.0, 10.0)];
iAmLost.backgroundColor = [UIColor redColor];
UIButton * iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 70, 200.0, 10.0) ];
[iAmLooking addTarget:self
action:#selector(iAmLookingScreen)
forControlEvents:UIControlEventTouchUpInside];
UIButton * about = [[UIButton alloc]initWithFrame:CGRectMake(0, 90, 200.0, 10.0)];
about.backgroundColor = [UIColor greenColor];
UIButton * howToUse = [[UIButton alloc]initWithFrame:CGRectMake(0, 120, 200.0, 10.0)];
howToUse.backgroundColor = [UIColor blackColor];
UIBarButtonItem* exit = [[UIBarButtonItem alloc] initWithTitle:#"Exit" style:UIBarButtonItemStyleBordered target:self action:#selector(exitApp:)];
NSArray *buttons = [[NSArray alloc]
initWithObjects:exit, nil];
toolbar.items = buttons;
[self.view addSubview:iAmLost];
[self.view addSubview:iAmLooking];
[self.view addSubview:howToUse];
[self.view addSubview:about];
[self.view addSubview:toolbar];

How can I know which view I am clicking? And why the button no reaction?

I am designing a viewcontroller which have several button bar, each bar canbe clicked and show a content view.like:
http://i.stack.imgur.com/m5V4Q.png
When I click the buttonbar, it's frame will expand bigger, and you can see the content in it(which is a button).
First, a bar button(320*30 size) and a contentView is a set, they are subviews of listview. And several listview makes the whole view.
Second, when I click it, the list view will expand from 320*30 to 320*180, the contentview in it will expand from 300*0 to 300 * 130(20 pixel of margin). and I set the clipsToBounds of contentview to Yes to make sure the content in contentview won't show when the frame height is 0.
Now,the click can show the contentview exactly as I want. But here is a problem: I can't click the button in it, and I tried to set their userInteractionEnabled to yes . And even I set the contentview as user-defined,which I set the view's userInteractionEnabled to yes and overwrite the touchbegin function to show a alertView. Those test are failed, no reaction found. And I checked and sure that there shouldn't be any view covered it.
What's the reason might be?
The code of setup the list view is:
NSArray *array = [titleArray objectAtIndex:i];
NSString *ShenSuoTitle = [array objectAtIndex:0];
NSString *ShenSuoContent = [array objectAtIndex:1];
UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
[listView setBackgroundColor:[UIColor blackColor]];
[listView setTag:[[NSString stringWithFormat:#"%d",(i+1)] intValue]];
[self.scrollView addSubview:listView];
totalHeight = totalHeight + 1 + ShenSuoViewHeight;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[titleView setBackgroundColor:[UIColor whiteColor]];
[titleView setTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
[listView addSubview:titleView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:[[NSString stringWithFormat:#"%d2",i+1] intValue]];
[btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[btn addTarget:self action:#selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
img.image = [UIImage imageNamed:#"list_ico.png"];
[img setTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
[titleView addSubview:img];
NSLog(#"img:%f,%f",img.frame.origin.y,img.frame.size.height);
UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
labTitle.textColor = [UIColor blackColor];
labTitle.backgroundColor = [UIColor clearColor];
labTitle.font = [UIFont boldSystemFontOfSize:15];
labTitle.text = ShenSuoTitle;
[titleView addSubview:labTitle];
NSLog(#"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);
//add a label for selected
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
selectedLabel.textColor = [UIColor grayColor];
//selectedLabel.alpha = 0.75;
selectedLabel.backgroundColor = [UIColor clearColor];
selectedLabel.text = #"All";
selectedLabel.font = [UIFont boldSystemFontOfSize:15];
[selectedLabel setTag:[[NSString stringWithFormat:#"%d5",i+1] intValue]];
[titleView addSubview:selectedLabel];
NSLog(#"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
content.backgroundColor = [UIColor grayColor];
UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
testLa.text = #"Label";
UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testBut.frame = CGRectMake(0, 40, 100, 30);
testBut.titleLabel.textColor = [UIColor greenColor];
testBut.titleLabel.text = #"Button";
content.tag = [[NSString stringWithFormat:#"%d4",i+1] intValue];
[content addSubview:testLa];
[content addSubview:testBut];
content.clipsToBounds = YES;
[testLa release];
[listView addSubview:content];
[content release];
[labTitle release];
[img release];
[titleView release];
[listView release];
the code handle the click to make the list view expands is:
UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];
UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d",i+1] intValue]];
listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;
UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:#"%d4",i+1] intValue]];
content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);
UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
img.image = [UIImage imageNamed:#"list_ico_d.png"];
I'm not sure I follow your situation exactly, and some code would be nice. To answer your question though, the reason is most likely that the touch is being intercepted elsewhere. It may also be that you just forgot to connect an action method to the button. Try using breakpoints or NSLogs to see what methods are being triggered and which aren't.
Thanks a lot. It is due to my fault. The code of expanding the bar which setting the listview's frame is wrong, it should add in the expanded height. This leads the visual is ok but the clicking not be handled.
So this problem closed.

Resources