I have a view with a collection of images. If the image is clicked then I show it full screen on same screen. When the phone is rotated, I want it to autorotate only if the image is full screen, otherwise no. I have isZoomedIn that is true when a image is full screen else false.
I have implemented the method shouldAutorotate. If it is zoomed-in it returns Yes otherwise No but it doesn't work even if isZoomedIn is set corectly.
I created a demo having a button to pop-up a new view on same screen similar to your image selection on clicking.
-(IBAction)buttonClicked:(id)sender //Called when button tapped to pop-up a new view.
{
flag = YES; //Flag check for rotating new view ,i.e., YES.
newView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, self.view.frame.size.width-10, self.view.frame.size.height-10)];
[newView setBackgroundColor:[UIColor blueColor]];
closeNewViewBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 30, 100, 50)];
[closeNewViewBtn addTarget:self action:#selector(removeView) forControlEvents:UIControlEventTouchUpInside];
[closeNewViewBtn setTitle:#"Remove View" forState:UIControlStateNormal];
[closeNewViewBtn setBackgroundColor:[UIColor redColor]];
[newView addSubview:closeNewViewBtn];
[self.view addSubview:newView];
}
-(void)removeView //Called to remove view or in your case change it for removing the image or what ever you doing.
{
flag = NO;
[newView removeFromSuperview];
}
-(BOOL)shouldAutorotate //Rotation check
{
if(flag)
return YES; //Will rotate when new view is present.
else
return NO; //Will not rotate when new view is not present.
}
This will get you required needs.
Related
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 created a MPMoviePlayerViewController in my UIView (not in my UIVIewController!) like this:
self.playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 70, 125, 100)];
[self.playButton setBackgroundImage:[UIImage imageNamed:#"video_play.png"] forState:UIControlStateNormal];
[self.playButton addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
self.playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
self.playerViewController.moviePlayer.fullscreen = NO;
self.playerViewController.view.frame = CGRectMake(0, 0, 320, 200);
[self.playerViewController.moviePlayer prepareToPlay];
self.playerViewController.moviePlayer.shouldAutoplay = NO;
self.playerViewController.view.backgroundColor = [UIColor yellowColor];
[self addSubview:self.playerViewController.view];
[self.playerViewController.view addSubview:self.playButton];
}
- (void)buttonPressed:(id)sender{
(NSLog(#"Click"));
[self.playerViewController.moviePlayer setFullscreen:YES animated:YES];
[self.playerViewController.moviePlayer play];
}
As you can see i added a Button on the videoView, beacause this part should only be a Preview and when the user clicks on the Button the MPMoviePlayerViewController should animate to fullscreen and start playing, and when the Video is finished it should go back to the Preview View. Everything works so far, but i have two Problems:
First Problem:
Everytime i open the View my StatusBar becomes hidden and it looks like this:
So i set in my viewWillAppear and viewDidAppear of my UIViewController:
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
This works, but now the statusbar gets hidden and immediately appears again, that looks ugly, any chance to solve this Problem?
Second Problem:
When i click on the Custom Button the Video gets fullscreen and everything works correct! But when i press the DONE Button of the Video everything goes back to the Preview Screen and it looks like this: The StatusBar is hidden, the navigationbar is also broken and there is a lot of black Space above the Video, whats the Problem here?
Ok i found a solution for this Problem, its a little bit hacky but i found no other solution. In my ViewController i do:
- (void)viewDidLoad {
[super viewDidLoad];
float delay = 0.1;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].statusBarHidden = NO;
});
and for the case User taps back Button i do:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[UIApplication sharedApplication].statusBarHidden = NO;
}
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'm creating a dynamic number of buttons(eventually, these will be placed inside a horizontal scroll view), and I'm displaying a popover when a button is pressed, but I need to set the popover to appear just to the right of the selected button, but at the moment, the popover appears at the same place every time...
This is what I'm trying:
for (int i=0; i < 10; i++) {
childButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
childButton.frame = CGRectMake(100*i, 170, 100, 30);
[childButton setTitle:#"Test" forState:UIControlStateNormal];
[childButton addTarget:self action:#selector(presentPopoverMenu) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:childButton];
}
Ultimately I'm going to create two rows of images, but this is just for testing...
-(void)presentPopoverMenu/*:(id)sender*/
{
MenuPickerController *mp = [[MenuPickerController alloc] init];
popoverMenu = [[UIPopoverController alloc] initWithContentViewController:mp];
popoverMenu.popoverContentSize = CGSizeMake(290, 300);
[popoverMenu presentPopoverFromRect:[childButton bounds]
inView:childButton
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:NO];
}
I'm thinking that it's probably just taking the value from last button, but I'm not sure, how I would do this otherwise...
Of course it uses last button value, because your ivar childButton points on it. Take not childButton but sender. Uncomment it and use. Also selector will be #selector(presentPopoverMenu:).
My application has splitview and it's activated only in landscape mode.
In masterview i have tableview and in the last section i have a custom button.
The problem is that the button width stays in potrait instead of using device orientation.
I've tried to play with setAutoresizingMask on button and view but that doesn't work either.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) sectionNum {
if (sectionNum == 4) {
CGRect frameSize;
if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
frameSize = CGRectMake(45.0, 0.0, 600.0, 44.0);
} else {
frameSize = CGRectMake(45.0, 0.0, 680.0, 44.0);
}
UIView *btnView = [[[UIView alloc] initWithFrame:frameSize] autorelease];
// button
UIButton* btnFind = [UIButton buttonWithType:UIButtonTypeCustom];
[btnFind setBackgroundImage:[[UIImage imageNamed:#"buttonblue.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0] forState:UIControlStateNormal];
[btnFind setBackgroundImage:[[UIImage imageNamed:#"buttonred.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0] forState:UIControlStateHighlighted];
btnFind.frame = frameSize;
btnFind.tag = 1;
[btnFind setTitle:#"Find" forState:UIControlStateNormal];
[btnFind addTarget:self action:#selector(doSearchDoc:) forControlEvents:UIControlEventTouchUpInside];
//[btnFind setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
//[btnView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[btnView addSubview:btnFind];
return btnView;
}
return nil;
}
Can someone please let me know how to resolve this.
Thanks.
I've had other issues that the header was not refreshed when rotating the device, ie. the method above was not invoked in my situatio, you could check if this is the case also for you.
The solution I adopted was to invoke reloadData in the rotating method :
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// This solves an issue that the header doesn't show when roating device (ok on simulator)
NSIndexPath* ipselected = [self.tableView indexPathForSelectedRow];
[self.tableView reloadData];
[self.tableView selectRowAtIndexPath:ipselected animated:false scrollPosition:UITableViewScrollPositionMiddle];
}
Of course it is usable if you can afford a full reload when rotating the device but itis typically a time when the user will better accept a 0.5s delay as he/she is busy with moving the device.
lternatively you could keep an ivar to your button and change its frame in the above method.