Add UISlider To UINavigationBar - ios

Trying to add a UISlider to UINavigationBar. I have it to where the slider shows, but when trying to change the value, it will not move. Here is the code in viewWillAppear. Note that this is on a child view and not the main parent view.
UIView *viewofslider = [[UIView alloc] initWithFrame:CGRectMake(160, 10, 40, 30)];
UISlider *theslider = [[UISlider alloc] init];
[theslider addTarget:self action:#selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[theslider setBackgroundColor:[UIColor clearColor]];
theslider.minimumValue = 50.0;
theslider.maximumValue = 150.0;
theslider.continuous = YES;
theslider.value = 100.0;
[viewofslider addSubview:theslider];
[self.navigationController.navigationBar addSubview:viewofslider];

To answer your specific question, I think the width of viewofslider is too narrow at 40.0, so the touch is not being picked up by the UISlider.
If you change the width of viewofslider to 100.0 you should be able to interact with it.
However, I agree with rmaddy's comment that you should simply add the slider to the navigation bar, there's no obvious need for the UIView.

Try to add it to titleView of view controller's navigation item in viewWillAppear:
UISlider *theslider = [[UISlider alloc] init];
...
self.navigationItem.titleView = theslider;

Related

How to I add a button on top of iCarousel?

I added the date label and the share button via code. Now I want to add a static button in the right top corner to open a slide out menu(I'm using MMDrawerController). If I'm adding the via code, it's moving with the carousel.
If I'm adding a button on the name label(My App) on the storyboard, it's not getting clicked and the carousel gesture is being registered instead of the button click.
How should I proceed to add a button for a slide out menu on the right ?
-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
UIView *magView = nil;
CGFloat height = self.myCarousel.bounds.size.height;
CGFloat width = self.myCarousel.bounds.size.width;
//Magazine View
magView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, width/1.35, height/1.75)];
magView.backgroundColor = [UIColor clearColor];
UIImageView *magImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"4 1:2.jpg"]];
magImage.frame = CGRectMake(0, 0, width/1.35, height/1.75);
[magView addSubview:magImage];
//Date Label
UILabel *dateLabel = nil;
dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, height/1.7, width/4, height/25)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.text = #"12-03-2017";
dateLabel.font = [UIFont fontWithName:#"Roboto-Black" size:5];
dateLabel.textColor = [UIColor whiteColor];
//Share Button
UIButton *shareButton = nil;
shareButton = [[UIButton alloc] initWithFrame:CGRectMake(width/1.49, height/1.7, height/25, height/25)];
shareButton.backgroundColor = [UIColor clearColor];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_icon_1x"] forState:UIControlStateNormal];
[magView addSubview:dateLabel];
[magView addSubview:shareButton];
return magView;
}
Simulator
Storyboard
How do I proceed to add the button ?
Add button to storyboard and after adding carousel programmatically. add code to bring the button to top on view.
[self.view bringSubviewToFront:button];
if you are adding it programmatically then just after [self.view addSubview:carousel];
call [self.view bringSubviewToFront:button](make sure button is added on view before calling it)
I solved the issue. Instead of taking the view of the ViewController I dragged/dropped another view onto the ViewController and displayed the carousel on it. Now I can set the button easily. Thanks for the help though. Cheers
It will help you
[xyzButton.superview setUserInteractionEnabled:YES];
:)

Add a sticky view underneath the Navigation Bar

I've seen a few other examples, but I haven't seen anything that actually sticks. What I want is essentially what a tableViewHeader does. I have a tableViewController with a navigation bar. I want to put a view into place that shows some search criteria in a small bar directly below the navbar. But I need it to stick when the user swipes to view the results.
I've tried adding a UIView as a subview to the navigation bar, but it always sits at the top of the Navigation Bar, overlaying everything.
Here's what worked:
// Create a custom navigation view
_navigationView = [[UIView alloc] init];
_navigationView.backgroundColor = [UIColor whiteColor];
_navigationView.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 44.0f,
self.view.frame.size.width,
30.0f);
// Create bottom border for the custom navigation view
_navigationViewBorder = [[UIView alloc] init];
_navigationViewBorder.backgroundColor = [UIColor darkGrayColor];
_navigationViewBorder.tag = 1;
_navigationViewBorder.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 74.0f,
self.view.frame.size.width,
0.5f);
// Add labels for date, results, and the time range
_dateDisplay = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 15)];
[_dateDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_dateDisplay setText:#""];
_timeRangeDisplay = [[UILabel alloc] initWithFrame:CGRectMake(98, 0, 135, 15)];
[_timeRangeDisplay setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:13]];
[_timeRangeDisplay setText:#""];
_resultsDisplay = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 80, 15)];
[_resultsDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_resultsDisplay setText:#""];
[_navigationView addSubview: _dateDisplay];
[_navigationView addSubview: _timeRangeDisplay];
[_navigationView addSubview: _resultsDisplay];
// Add two views to the navigation bar
[self.navigationController.navigationBar.superview insertSubview:_navigationView belowSubview:self.navigationController.navigationBar];
[self.navigationController.navigationBar.superview insertSubview:_navigationViewBorder belowSubview:_navigationView];
Why not add the view to your viewController's view, and set up its constraints so that it sits just below the navigationBar, but above the tableView?
If it's only supposed to be there some of the time, you can animate its constraints to show/hide it.

My UIView has a UIButton as one of it's subviews but not responding to events in objective-c

Here is my code. It has started to look crowded after an hour or 2 of trying to solve this issue. I've tried setting user interaction enabled to yes, I've tried just using a button alone and not making it the subview of another view.
Right now I have filterBar > filterBarContainer > filterButton.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.navigationBar.userInteractionEnabled = YES;
self.view.userInteractionEnabled = YES;
// Create filter bar with specified dimensions
UIView *filterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 42, self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
[filterBar setUserInteractionEnabled:YES];
// Make it a subview of navigation controller
[[[self navigationController] navigationBar] addSubview:filterBar];
[filterBar setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.9f]];
[filterBar setTag:1];
// Reusable vars
// CGFloat filterBarX = filterBar.frame.origin.x;
//CGFloat filterBarY = filterBar.frame.origin.y;
CGFloat filterBarHeight = filterBar.frame.size.height;
CGFloat filterBarWidth = filterBar.frame.size.width;
// Create centre filter button with specified dimensions
UIView *filterButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 2, filterBarWidth / 2 - 30 , filterBarHeight - 10)];
[filterButtonContainer setUserInteractionEnabled:YES];
// Make it a subview of filter bar
[filterBar addSubview:filterButtonContainer];
[filterButtonContainer setBackgroundColor:[UIColor redColor]];
// Centre the button
[filterButtonContainer setCenter:[filterBar convertPoint:filterBar.center fromView:filterBar.superview]];
// Add button to filter button
UIButton *filterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[filterButton setUserInteractionEnabled: YES];
[filterButton setFrame:CGRectMake(0, 0,40 , 20)];
[filterButton setTitle:#"test" forState:UIControlStateNormal];
[filterButtonContainer addSubview:filterButton];
[filterButton setBackgroundColor:[UIColor yellowColor]];
// self.filterButton = filterButton;
[filterButton addTarget:self action:#selector(filterButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
NSLog(#"dd");
filterButtonContainer.layer.borderColor = [UIColor blackColor].CGColor;
filterButtonContainer.layer.borderWidth = 1;
This is the method I'm trying to trigger.
- (void)filterButtonTapped:(UIButton *)sender
{
NSLog(#"filter button tapped");
UIActionSheet *filterOptions = [[UIActionSheet alloc] initWithTitle:#"Filter Garments"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Recommended", #"What's New", #"Price - High to Low", #"Price - Low to High", nil];
[filterOptions showInView:self.collectionView];
}
Most likely one of your parent views (superviews of your button) have a width or height that places the filter button outside of their area. The button is still displayed (if you don't define that the view should cut off subviews), but elements outside the view's area will not get any touch events. Check your superview's width and height.
You're adding filterBar as a subview of your navigationBar, but you're setting it's y-origin within navigationBar to 42. And then you're setting your filterButtonContainer's y-origin within the filterBar to 2. Assuming navigationBar's height is 44, most of the filterBar and the entirety of your filterButtonContainer and filterButton won't be within their navigationBar superview so the button wouldn't be selectable.
Edit: To make it selectable while keeping the positioning intact, I suggest adding the filter bar to self.view. For example, change:
[[[self navigationController] navigationBar] addSubview:filterBar];
to
[self.view addSubview:filterBar];

UIView animation causes a UIButton to jump

UI is programmatically generated in my app. I have few buttons and texboxes in the UI. I have developed a custom uidatapicker object, which pops up from the top and animates to the middle of the screen. When uidatapicker pops up I draw another UIView(called helper view) with the size of the screen so all other uiobjects in the screen except uidatepicker become disabled.
But when the UIDatePicker is animating a button in the UI jumps to another location. Also I have three buttons in my UI. It happens with only one button(one UIButon in the UIView). Other two buttons are ok. Also there is no significant difference between those buttons except the button text.
I removed the earlier described view(helper view), but still the
problem is occurring.
I need to know why this is occurring how to
prevent it.
Also I have lot of other pages which works fine.
The code
-(void)openEditDateTime:(UIDatePickerMode) mode {
if ([clientView viewWithTag:9]) {
[self cancelPressed];
}
CGRect toolbarTargetFrame = CGRectMake((clientView.frame.size.width/2)-160, (clientView.frame.size.height/2)+91, 320, 44);
CGRect datePickerTargetFrame = CGRectMake((clientView.frame.size.width/2)-160, (clientView.frame.size.height/2)-125, 320, 216);
backgroundView = [[UIView alloc] initWithFrame:clientView.bounds];
backgroundView.alpha = 0;
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(cancelPressed:)];
[backgroundView addGestureRecognizer:tapGesture];
[clientView addSubview:backgroundView];
[self bringToFront:backgroundView];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.bounds.size.height+44, 320, 216)];
datePicker.tag = 10;
datePicker.datePickerMode = mode;
[clientView addSubview:datePicker];
[clientView bringSubviewToFront:datePicker];
[datePicker becomeFirstResponder];
[self bringToFront:datePicker];
if(date != nil){
datePicker.date = date;
}
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.bounds.size.height, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(donePressed:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelPressed:)];
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, cancelButton, nil]];
[clientView addSubview:toolBar];
[clientView bringSubviewToFront:toolBar];
[UIView beginAnimations:#"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
backgroundView.alpha = 0.5;
[UIView commitAnimations];
}
- clientView is a UIScrollView.
- backgroundView is the helper view described earlier.
This is how I add buttons. I wil put only a part of the button rendering code as putting all the code is unnecessary and it has lot other dependencies as well.
-(RenderedElement*)renderElement:(NSObject*)element:(ParentView*)parent:(PageView*)page:(Page*)modelPage:(RenderedElement*)parentRenderedElement {
UIButton *buttonView = nil;
Button *templateElement = nil;
buttonView = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonView setLineBreakMode:UILineBreakModeWordWrap];
[buttonView addTarget:parent action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[parent addSubview:buttonView];
}
UPDATE
When I change the order of rendering and if I render the buttons first the jumping effect is not happening. The UI works fine. It temporally solves the problem. But I want find the reason and have a better solution.
Maybe you can add
[UIView setAnimationBeginsFromCurrentState:YES];
after
[UIView beginAnimations:#"MoveIn" context:nil];
Did you actually say which button is moving? Kind of hard to help without some more detail. But here are some suggestions to look at:
Rather than adding the datepicker to the bottom of the stack and then pulling it forward:
[clientView addSubview:datePicker];
[clientView bringSubviewToFront:datePicker];
Why don't you try to try adding it by:
[clientView insertSubview:datePicker atIndex:0];
Also, you are adding the toolbar, which I assume contains the offending button, after adding the datPicker and then pulling that forward. Do you want the toolbar to be above the datepicker in the view hierachy?
Perhaps insert it at:
[clientView insertSubview:toolBar aboveSubview:datePicker];
Finally, do you need to animate the toolbar frame?
The only thing i can imagine is that may be you have this button on the client view. and since you are adding and moving this
[clientView addSubview:datePicker];
[clientView bringSubviewToFront:datePicker];
this may cause readjusting of your UIButton's frame. and since that happens during this procedure it also animates its movement which may appear as a jumping button..so i guess you should try
[jumpingButton setAnimationsEnabled:NO]; //you-know jumping button is the outlet of the -you-know-who

Embedding a CGRect in Navigation Bar

Is it possible to embed a CGRect into a navigation bar just like this screenshot: here
I've done this:
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 10, 10)];
[self.view addSubview:aView];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(100, -15, 150, 10)];
Using a negative figure, it actually allows me to place the label in the same space as the nav bar, but it will be hidden behind it. Is there way for me to bring the label to the front (change the z-index?)? Or is there a way to embed a label in the nav bar?
I've tried using this code but it doesn't effect the label:
[self.view bringSubviewToFront:aView];
Answered thanks to EmptyStack:
Adding this code:
self.navigationItem.titleView = aView;
[aView addSubview:title];
Allows to embed the title (or more) in the navigation bar.
I guess you want this one.
self.navigationItem.titleView = aView;

Resources