adding a button to navigation bar on first nib viewController and wanted to view this button to only on main screen not on any other screen
i have worked out on it
i made the button and added to subview of navigation bar
on every button action i have put this
- (IBAction)forth:(id)sender {
forthView *forthview = [[forthView alloc]init];
[self.navigationController pushViewController:forthview animated:YES];
btn.hidden = YES;
}
after this button hides but didnot show up when i got back to main screen
my code for viewdidload is here
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = #"My First View";
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
btn = [[UIButton alloc] init];
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 65, 30);
[btn setTitle:#"Show" forState:UIControlStateNormal];
[self.navigationController.navigationBar addSubview:btn];
btn.hidden = false;
}
Once you hide the button so you need to unhide it again. here is the simple code to unhide the button:
btn.hidden = No
viewDidLoadis only called the first time your view is loaded. Add an NSLog()statement if you want to test when it gets called. Every time your view appers on screen viewWillAppearis called.
So you need to show the button in viewWillAppear
It seems likely that what you really want to be doing is adding a UIBarButtonItem to your main view controller's navigationItem.leftBarButtonItem property. That way the button will have the correct appearance and show and hide as the view controller is displayed.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Show"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(onShowButtonPressed:)];
Related
I have a uisegementcontrol and a uibarbutton that gets added to the navigation bar of the navigation controller. When I push to the next view, however, I want them to be removed. I tried several approaches, but nothing seems to be removing them. One approach was using viewWillDisappear and having the segementcontrol and the bar button removedFromSuperView. Another was to call the removeFromSuperView from the method that pushes to the next scene. Nothing seems to be working. I've actually been stuck on this for a really long time!
Also, the view controller "X" that has the segement control and the bar button is presented through a presentViewController. X then pushes the other view controller.
Here is the code for the segement control
NSArray *headers = #[#"Tracks", #"Playlists"];
self.savedMusic = [[UISegmentedControl alloc] initWithItems:headers];
CGSize size = self.view.frame.size;
CGRect segFrame = CGRectMake(80, 5, size.width - 140, 35);
self.savedMusic.backgroundColor = [UIColor blackColor];
self.savedMusic.frame = segFrame;
self.savedMusic.tag =1;
[self.savedMusic setTitle:#"Tracks"
forSegmentAtIndex:0];
[self.savedMusic setTitle:#"Playlists"
forSegmentAtIndex:1];
[self.savedMusic addTarget:self
action:#selector(whatToDisplay)
forControlEvents:UIControlEventValueChanged];
self.savedMusic.selectedSegmentIndex = 0;
[self.navigationController.navigationBar addSubview:self.savedMusic];
Here is the code for the viewWillDisapear
[super viewWillDisappear:animated];
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
self.navigationItem.leftBarButtonItem = nil;
[self.savedMusic removeFromSuperview];
And here is the code for the method that switches to the new view
PlaylistTracksTableViewController *vc = [[PlaylistTracksTableViewController alloc] init];
[self.savedMusic removeFromSuperview];
[[self.navigationController.navigationBar viewWithTag:1] removeFromSuperview];
self.navigationItem.leftBarButtonItem = nil;
[self.navigationController pushViewController:vc animated:YES];
The problem is that what you are doing is not how you manipulate the contents of the navigation bar. Do not talk to self.navigationController.navigationBar. Instead, each view controller should set its own self.navigationItem, and the runtime will use that to populate the navigation bar for that view controller.
How can I do something like this:
I tried to changed through setframe and nothing happened:
UIImage *flechSuiv = [UIImage imageNamed:#"picto_fleche_lieux_suiv.png"];
UIButton *buttonSuiv = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonSuiv setImage:flechSuiv forState:UIControlStateNormal];
[buttonSuiv addTarget:self action:#selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[buttonSuiv setFrame:CGRectMake(0, 34+flechSuiv.size.width, flechSuiv.size.width,flechSuiv.size.height)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:barButtonSuiv,barButtonPrec,nil];
self.navigationController.navigationBar.translucent = NO;
I think you should go for a custom navigation bar. You'd need to hide the navigation bar before presenting the view.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
And put an UIView matching the size of a regular tab bar in your view controller.
Inside of that view, you can put any custom buttons with regular action (push or pop).
I need to add a custom button to the AVPlayerViewController that will appear in both fullscreen and non-fullscreen for an app running iOS 8.
Adding a button to the AVPlayerViewController.view or the containing view will work for non-fullscreen but when the player switches to fullscreen the button is no longer visible. I have found that if I add a button to the AVPlayerViewController.ContentOverlayView then it appears in fullscreen and non-fullscreen, but then it doesn't appear that the ContentOverlayView responds to any touches so the button cannot be clicked. Does anyone know of a different place to add the button or a way to make the ContentOverlayView respond to touches?
Example Code
AVPlayerViewController *playerView = [[AVPlayerViewController alloc] init];
playerView.player = [AVPlayer playerWithURL:movieURL];
CGRect viewInsetRect = CGRectInset ([self.view bounds],
kMovieViewOffsetX,
kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[playerView view] setFrame:viewInsetRect];
[self.view addSubview: [playerView view]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor yellowColor];
btn.frame = CGRectMake(50, 50, 200, 75);
[btn addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[btn setEnabled:YES];
[playerView.contentOverlayView addSubview:btn];
I managed to get this working by adding the following to my AVPlayerViewController in IB:
An overlay view with a button inside it
Add constraints to center the button in the overlay view
Created an IBOutlet to the view (I called it 'overlay')
Created an action from the button to AVPlayerViewController that prints something so that we can see that tapping the button is working
Dragged the overlay view out of the view hierarchy and onto the top bar of the view controller (appears as a view icon in top bar of the view controller along with the exit icon etc)
and then in the code for the AVPlayerViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.overlay)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.overlay.frame = self.view.bounds
}
#IBAction func playBtnTapped(sender: AnyObject) {
println("PlayBtnTapped")
}
The button is now centered in fullscreen/normal viewing and the button works
I had the same predicament just now.
Instead of:
[playerView.contentOverlayView addSubview:btn];
use (after you add the playerView)
[self.view addSubview:btn];
This will add the button in the minimised player.
Now for the fullscreen I didn't found any other reliable method of checking if the player went fullscreen or back, so here is a a solution:
self.windowChecker = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(checkIfFullscreen) userInfo:nil repeats:YES];
and this:
-(void)checkIfFullscreen{
NSLog(#"videoBounds\n %f %f ", self.avVideoPlayer.contentOverlayView.frame.size.width,self.avVideoPlayer.contentOverlayView.frame.size.height);
if ([self playerIsFullscreen]) { // check the frame on this
for(UIWindow* tempWindow in [[UIApplication sharedApplication]windows]){
for(UIView* tempView in [tempWindow subviews]){
if ([[tempView description] rangeOfString:#"UIInputSetContainerView"].location != NSNotFound){
UIButton *testB = [[UIButton alloc] initWithFrame: CGRectMake(20, 20, 400, 400)];
testB.backgroundColor = [UIColor redColor];
[testB addTarget:self action:#selector(buttonTouch) forControlEvents:UIControlEventTouchDown];
[tempView addSubview:testB];
break;
}
}
}
}
}
I know this is a not a nice way to do it, but it is the only way I managed to add some custom UI that also receives touch events on the player.
Cheers!
I’m adding a UISegmentedControl right under the NavigationBar in a UITableViewController. This is the code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationBar = self.navigationController.navigationBar;
UIView *segmentView=[[UIView alloc] initWithFrame:CGRectMake(0, self.navigationBar.frame.size.height, self.navigationBar.frame.size.width, 50)];
[segmentView setBackgroundColor:[UIColor whiteColor]];
segmentView.alpha = 0.95;
self.tabSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Favourites", #"All", nil]];
self.tabSegmentedControl.frame = CGRectMake(20, 10, self.navigationBar.frame.size.width - 40, 30);
[self.tabSegmentedControl addTarget:self action:#selector(tabChanged:) forControlEvents:UIControlEventValueChanged];
[segmentView addSubview:self.tabSegmentedControl];
[self.navigationBar addSubview:segmentView];
[self.tabSegmentedControl setSelectedSegmentIndex:1];
}
The view and the SegmentedControl appear on the screen well, but they are not clickable. The selector doesn’t get executed when tapped on the SegmentControl; it doesn’t even switch tabs! In fact, the stuff that is underneath the segmentView (items in the TableView) get clicked when you tap on it. I have tried but failed to understand why this is happening! Any suggestions would be helpful!
You are adding a view below the bounds of its super view. You may see the view however you cannot click it because it is out of bounds. If you set the property of the navigation bar clipsToBounds to YES you should see that the view disappears. What you need to do is add the segment controller to the table view. Here is an example:
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self.view addSubview: self.segmentView]; // need to keep a pointer to segmentView
self.tableView.contentInset = UIEdgeInset(self.segmentView.frame.size.height, 0,0,0);
}
-(void) scrollViewDidScroll:(UIScrollView*) scrollView{
CGRect rect = self.segmentView.frame;
rect.origin = self.tableView.contentOffset;
self.segmentView.frame = rect;
}
I wish to add a UIToolbar programmatically to a view when a user clicks on a button (in my case when they zoom in on a photo).
It seems to work fine when I create the toolbar in the click method and add to the subview but if I create the toolbar in the viewDidLoad method, assign it to an instance variable,and add that instance variable later to the subview on click, nothing appears. The debugger shows that the instance variable is a UIToolbar and is not null. I didn't want to create and destroy the same toolbar on every click so I thought it was better just to keep it as an instance variable that I add and remove from the view as needed. Is this the right approach?
Why is it visible in the one case and not the other.
Setup
#synthesize toolBar;
- (UIToolbar*)createToolbar
{
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(sharePhoto:)];
NSArray *buttonItems = [NSArray arrayWithObjects:shareButton,nil];
[toolbar setItems:buttonItems];
return toolbar;
}
This works
- (void) clickMyButton {
toolBar = [self createToolbar];
[self.view addSubview:toolBar];
}
This doesn't show anything
- (void)viewDidLoad
{
[super viewDidLoad];
toolBar = [self createToolbar];
}
- (void) clickMyButton {
[self.view addSubview:toolBar];
}
Why doesn't it work in the latter case
The problem is that when viewDidLoad gets called, it is not guaranteed that the frames for your view and subviews are set. Try calling [self createToolbar] from viewWillAppear or viewDidAppear instead.