I have a ViewController with a NavigationController and I want to add a translucent UIView with some Buttons over the ViewController when I press a ViewController button, the problem is that I can not put the UIView over the NavigationBar. How can I solve this?
This is my code ( Very simple)
-(void)setOpacityView
{
opacityVw = [[UIView alloc] initWithFrame:self.view.bounds];
opacityVw.backgroundColor = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
WPCustomButton *closeBtn = [[WPCustomButton alloc] initWithFrame:CGRectMake(230, 10, 80, 20)];
[closeBtn setTitle:#"Close X" forState:UIControlStateNormal];
[closeBtn setBackgroundColor:[UIColor clearColor]];
[closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[closeBtn addTarget:self action:#selector(closeView) forControlEvents:UIControlEventTouchUpInside];
[opacityVw addSubview:closeBtn];
}
// ---------------------------------------------------------------------------------------------------------------------
#pragma mark - Button methods
-(void) closeView
{
[opacityVw removeFromSuperview];
}
-(void)setProfileImage
{
[self setOpacityView];
[self.view addSubview:opacityVw];
}
I answered a similar question here
Try something like this:
-(void)setProfileImage
{
[self setOpacityView];
[self.navigationController.view addSubview:opacityVw];
}
Add it to the AppDelegate's UIWindow instead.
- (void)setProfileImage
{
[self setOpacityView];
[ [[UIApplication sharedApplication] delegate].window addSubview:opacityVw];
}
Don't forget to change your view size:
opacityVw = [[UIView alloc] initWithFrame:[[[UIApplication sharedApplication] delegate]window].bounds];
You can create MainViewController and put that as your window.rootViewController. Add your navigationController to this MainViewController. After that it you add the view to your mainViewController, it would be on top of navigation Controller.
Just make it simple :
-(void)setProfileImage
{
[self setOpacityView];
self.navigationController.navigationBarHidden = YES;
[self.view insertSubview:opacityVw aboveSubview:self.view];
}
-(void) closeView
{
[opacityVw removeFromSuperview];
self.navigationController.navigationBarHidden = NO;
}
Related
I have added a UIView to my [UIApplication sharedApplication].keyWindow and it is working properly.
When a button on the UIView is tapped, I want to push a new UIViewController onto the underlying NavigationController
This all works, but how can I make the UIView animate off the screen, to the left, with the underlying UIViewController ?
You can animate your custom view and at the same time pass the control to the mail view controller to push the next view controller onto the navigation controller's stack... Below is the example code which I have simulated. The animation of your custom view should be in sync with the view controllers animation.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_v = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 50, 30)];
[btn setTitle:#"Button" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[_v addSubview:btn];
_v.backgroundColor = [UIColor redColor];
[[[UIApplication sharedApplication] keyWindow] addSubview:_v];
}
- (void)btnClicked:(id)sender {
NSLog(#"Btn Clicked");
[UIView animateWithDuration:0.2
delay:0.1
options: UIViewAnimationOptionCurveEaseOut
animations:^
{
_v.frame = CGRectMake(-_v.frame.size.width,
_v.frame.origin.y, _v.frame.size.width, _v.frame.size.height);
}
completion:^(BOOL finished)
{
}];
UIViewController *c = [self.storyboard instantiateViewControllerWithIdentifier:#"TestVC"];
[self.navigationController pushViewController:c animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
I have created a custom UIView to show an UIActivityIndicatorView and a message. Idea is to reuse this across my app for consistency. Below is the code:
#implementation CDActivityIndicator
#synthesize activityIndicator, message;
//init method called when the storyboard wants to instantiate this view
- (id) initWithCoder:(NSCoder*)coder {
if ((self = [super initWithCoder:coder])) {
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
message = [[UILabel alloc] initWithFrame:CGRectZero];
[message setBackgroundColor:[UIColor clearColor]];
[message setTextColor:[UIColor whiteColor]];
[message setTextAlignment:NSTextAlignmentCenter];
[message setFont:[UIFont fontWithName:#"HelveticaNeue" size:15.0f]];
[self addSubview:activityIndicator];
[self addSubview:message];
//set background color
[self setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
}
return self;
}
-(void) layoutSubviews {
[super layoutSubviews];
//position the indicator at the center of the view
[activityIndicator setCenter:[self center]];
//position the label
[message setFrame:[self frame]];
}
- (void) begin {
//make sure the current view is visible, and message is hidden
[self setHidden:NO];
[activityIndicator setHidden:NO];
[self bringSubviewToFront:activityIndicator];
[message setHidden:YES];
[self performSelectorOnMainThread:#selector(startAnimating) withObject:self waitUntilDone:YES];
}
- (void) startAnimating {
if( !activityIndicator.isAnimating ) {
[activityIndicator startAnimating];
}
}
To try this out, I added a UIView to one of my views in the storyboard and set the class for that view to CDActivityIndicator. When I call begin() from the corresponding View Controller, the CDActivityIndicator gets shown as an empty view with the expected background color. However, the activity indicator doesn't show. All methods are getting called as expected.
Any idea what I might be missing? Thanks!
You should change your subviews frame setting like this.
CGPoint point = [self.superview convertPoint:self.center toView:self];
[activityIndicator setCenter:point];
[message setFrame:self.bounds];
The frame defines the origin and dimensions of the view in the coordinate system of its superview. Every UIView has its own coordinate system. You should take this into account.
I am trying to remove a facebook view controller that I created. Basically, the showFBLoginView method that initializes the view controller is the App Delegate and called within a GameOver layer.
-(void)showFBLoginView {
facebookViewController = [[FacebookViewController alloc] init];
self.facebookViewController = facebookViewController;
// Set loginUIViewController as root view controller
[[self window] setRootViewController:facebookViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self.facebookViewController activateButton:#"Facebook Connect"];
.
.
.
}
The activateButton method within the facebookViewController.m class:
-(void)activateButton:(NSString*)buttonText {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
fbConnectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
fbConnectButton.frame = CGRectMake(screenSize.width/2 - 50*(screenSize.width/480) , screenSize.height/2, screenSize.width/4, screenSize.height/12);
[fbConnectButton setTitle:buttonText forState:UIControlStateNormal];
fbConnectButton.backgroundColor = [UIColor redColor];
[fbConnectButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal ];
[fbConnectButton addTarget:self action:#selector(callButtonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:fbConnectButton];
}
I tried to remove the faceboookViewController in the following way. This was done within the App Delegate.
[[self window] removeFromSuperview];
But it's not working. What is the right way to remove the view controller?
faceboookViewController *fbViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"StoryboardID"];
[self dismissViewControllerAnimated:YES completion:nil];
StoryboardID is StoryboardID of faceboookViewController.
you can dismiss if you present it.
How to enter storyboard's initial view when click the button in the scroll view.
My storyboard consists of the following:
[Navigation Controller] -> [TableView Controller] -> [DetailView Controller]
My code:
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
[self GuidePage];
return YES;
}
-(void)GuidePage
{
UIScrollView *scro=[[UIScrollView alloc]initWithFrame:self.window.bounds];
scro.pagingEnabled=YES;
_currentScro=scro;
scro.scrollEnabled=YES;
scro.bounces=NO;
scro.showsHorizontalScrollIndicator=NO;;
scro.delegate=self;
UIImageView *imgView1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Guide1.png"]];
imgView1.frame=CGRectMake(0, 0, 320, 480);
UIImageView *imgView2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Guide2.png"]];
imgView2.frame=CGRectMake(320, 0, 320, 480);
UIButton *btnEnter= [[UIButton alloc] initWithFrame:CGRectMake(400, 200, 100, 100)];
[btnEnter setTitle:#"Enter" forState:UIControlStateNormal];
[scro addSubview:imgView1];
[scro addSubview:imgView2];
[scro addSubview:btnEnter];
[btnEnter addTarget:self action:#selector(gotoMainStoryboard) forControlEvents:UIControlEventTouchUpInside];
scro.contentSize=CGSizeMake(320*2, scro.frame.size.height);
UIPageControl *page=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 400, 320, 20)];
page.numberOfPages=2;
_pageControl=page;
[page addTarget:self action:#selector(changeCurrentPage:) forControlEvents:UIControlEventValueChanged];
page.backgroundColor=[UIColor redColor];
page.currentPage=0;
[self.window addSubview:scro];
[self.window addSubview:page];
}
-(void) gotoMainStoryboard
{
NSLog(#"goto");
}
-(void)changeCurrentPage:(UIPageControl *)sender
{
[_currentScro setContentOffset:CGPointMake(sender.currentPage*320, 0)];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ((int)scrollView.contentOffset.x%320==0) {
_pageControl.currentPage=(int)scrollView.contentOffset.x/320;
}
}
#end
In your
-(void) gotoMainStoryboard
{
[self performSegueWithIdentifier:#"segue name" sender:nil];
}
This will bring next viewcontroller on button click.
I am trying to adapt my application for iOS 7. The issue I am having is I can not change the tint color of some controls.
I did add
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if (IOS7_OR_LATER)
self.window.tintColor = [self greenTintColor];
to my app delegate's
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
It mostly helped but color of message box and action sheet buttons is still the default blue.
How can I recolor all such buttons too?
Some screenshots:
As UIAlertView is deprecated You can. Use UIAlertController.
You can use tintColor property.
OLD
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
-From Apple Doc
You can use tintColor property or You can use Some Custom Library for that, you can find it at cocoacontrols.com.
I was able to change the cancel button's text color to white in app delegate.
[[UIView appearance] setTintColor:[UIColor whiteColor]];
For Actionsheet You can use
Utilize the willPresentActionSheet delegate method of UIActionSheet to change the action sheet button color.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
button.titleLabel.textColor = [UIColor greenColor];
}
}
}
Combining best answers above, and updated for deprecation:
[[UIView appearanceWhenContainedInInstancesOfClasses:#[[UIAlertController class]]] setTintColor:[UIColor greenColor]];
or Swift:
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .green
Works in 2018, Swift 4 / iOS 12.
You can adjust the color by searching and modifying the UILabel in the subview hierarchy of the alert window that is created right after showing the alert:
- (void)setButtonColor:(UIColor*)buttonColor {
dispatch_after(dispatch_time(0,1), dispatch_get_main_queue(), ^{
NSMutableArray *buttonTitles = [NSMutableArray array];
for (NSUInteger index = 0; index < self.numberOfButtons; index++) {
[buttonTitles addObject:[self buttonTitleAtIndex:index]];
}
for (UILabel *label in [[[UIApplication sharedApplication] keyWindow] recursiveSubviewsOfKind:UILabel.class]) {
if ([buttonTitles containsObject:label.text]) {
label.textColor = buttonColor;
label.highlightedTextColor = buttonColor;
}
}
});
}
[alert show];
[alert setButtonColor:UIColor.redColor];
The recursiveSubviewsOfKind: method is a category on UIView that returns an array of views in the complete subview hierarchy of the given class or subclass.
for UIAlertView with colored buttons you can use the cocoapod "SDCAlertView"
about CocoaPods: http://www.cocoapods.org
how to install CocoaPods: https://www.youtube.com/watch?v=9_FbAlq2g9o&index=20&list=LLSyp50_buFrhXC0bqL3nfiw
In iOS 6.0 create custom view in App delegate
.h
UIView* _loadingView;
UIView* _subView;
UIActivityIndicatorView*loadingIndicator;
UITabBarController *tabBar_Controller;
NSTimer *timer;
#property (strong, nonatomic) UIView* _loadingView;
#property (strong, nonatomic) UIView* _subView;
.m- (void)fadeScreen
{
[UIView beginAnimations:nil context:nil]; // begins animation block
[UIView setAnimationDuration:3.0]; // sets animation duration
[UIView setAnimationDelegate:self]; // sets delegate for this block
[UIView setAnimationDidStopSelector:#selector(finishedFading)];
self.txtview.alpha = 0.0; // Fades the alpha channel of this view
[UIView commitAnimations]; // commits the animation block. This
}
- (void) finishedFading
{
[self.txtview removeFromSuperview];
}
- (void)showConnectivity:(NSString *)strTitle
{
[_loadingView setBackgroundColor:[UIColor clearColor]];
[_loadingView setAlpha:0.5];
[_loadingView.layer setCornerRadius:10];
[self.window addSubview:_loadingView];
[_loadingView setHidden:NO];
[_subView.layer setCornerRadius:7];
[_subView setBackgroundColor:[UIColor colorWithHue:0.0f saturation:0.0f brightness:0.0f alpha:0.6]];
[_subView setOpaque:YES];
[self.window addSubview:_subView];
[_subView setHidden:NO];
[_loadingView setHidden:NO];
[_subView setHidden:NO];
loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loadingIndicator setFrame:CGRectMake(85,10,35,35)];
[_subView addSubview:loadingIndicator];
[loadingIndicator setBackgroundColor:[UIColor redColor]];
[loadingIndicator startAnimating];
UILabel *_lab=[[UILabel alloc]initWithFrame:CGRectMake(8,10,72,45)];
[_lab setText:strTitle];
[_lab setTextColor:[UIColor whiteColor]];
[_lab setBackgroundColor:[UIColor clearColor]];
[_lab setFont:[UIFont boldSystemFontOfSize:13.0]];
[_lab setTextAlignment:NSTextAlignmentCenter];
[_subView addSubview:_lab];
}
- (void)CoonectingViewHidden
{
[_loadingView setHidden:YES];
[_subView setHidden:YES];
NSArray *_aryViews = [_subView subviews];
for(int i = 0; i<[_aryViews count];i++)
{
id obj = [_aryViews objectAtIndex:i];
if(![obj isKindOfClass:[UIActivityIndicatorView class]])
[obj removeFromSuperview];
}
[loadingIndicator stopAnimating];
[loadingIndicator hidesWhenStopped];
}
in using .m
#import"Appdelegate.h"
- (void)showLoadingIndicator:(NSString *)message
{
AppDelegate *delegateObj2=(AppDelegate *)[UIApplication sharedApplication].delegate;
[delegateObj2 showConnectivity:message];
}
-(void)stopLoading
{
AppDelegate *delegateObj3=(AppDelegate *)[UIApplication sharedApplication].delegate;
[delegateObj3 CoonectingViewHidden];
}
// [self showLoadingIndicator:#"Loading"];
n
[self stopLoading];