titleView jumps after segue - ios

In my app I have a UINavigationController with a UISplitViewController as rootViewController. The masterViewController of this UISplitViewController sets a UIView with a UITextField centered inside it as titleView. This is wat it looks like in the storyboard.
- (void)viewDidLoad {
[super viewDidLoad];
self.titleView = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-((self.view.frame.size.width/2)+150))/2, 0, (self.view.frame.size.width/2)+150, 30)];
self.rightButton = [[UIButton alloc] init];
[self.rightButton setImage:newImage forState:UIControlStateNormal];
[self.rightButton addTarget:self action:#selector(barButtonRight:) forControlEvents:UIControlEventTouchUpInside];
self.rightButton.frame = CGRectMake(0, 0, 30, 30);
self.rightButton.tintColor = [UIColor whiteColor];
[self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
self.splitViewController.navigationItem.rightBarButtonItem = rightButton;
self.textField = [[UITextField alloc]init];
if (IDIOM == IPAD) {
self.textField.frame = CGRectMake(0, 0, self.titleView.frame.size.width, 30);
} else {
self.textField.frame = CGRectMake(65, 0, (self.view.frame.size.width-20)-110, 30);
}
self.textField.backgroundColor = [UIColor purpleColor];
if (IDIOM == IPAD) {
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];
self.textField.leftView = paddingView;
self.textField.leftViewMode = UITextFieldViewModeAlways;
}
[self.titleView addSubview:self.textField];
self.splitViewController.navigationItem.titleView = self.titleView;
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
Now, when a the right button is pressed a strange thing happens.
- (IBAction)barButtonRight:(id)sender {
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
The UITextField is pushed all the way to the left inside the UIView. I commented out most of my viewDidLoad code and this is still happening. Any ideas as to what's causing this?

Related

IOS 11 Navigation Bar Button Item

We were using the below code to show leftbarbutton item in our navigation bar in IOS 10 SDK and XCode 8, however when we updated to xcode9 and ios 11 sdk. The left bar button item doesnt show. I searched through the web , but couldnt find any solution to that.
Does anyone know any answer?
UIBarButtonItem *cancelButton = nil;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"11")) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
//button.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
//button.contentEdgeInsets = UIEdgeInsetsMake(13, 8, 13, 8);
NSLayoutConstraint *widthConst = [button.widthAnchor constraintEqualToConstant:32.0];
NSLayoutConstraint *heightConst = [button.heightAnchor constraintEqualToConstant:32.0];
[button setImage:[UIImage imageNamed:#"cross"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
cancelButton = [[UIBarButtonItem alloc] initWithCustomView:button];
widthConst.active = YES;
heightConst.active = YES;
} else {
cancelButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"cross"] style:UIBarButtonItemStylePlain target:loginRouter action:#selector(hide)];
}
cancelButton.tintColor = [UIColor whiteColor];
loginViewController.navigationItem.leftBarButtonItem = cancelButton;
loginRouter.loginViewController = loginViewController;
CardNavigationController *navigationController = [CardNavigationController darkBlueNavigationControllerWithClient:client];
navigationController.viewControllers = #[ loginViewController ];
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[onViewController presentViewController:navigationController animated:YES completion:nil];
And you can find the result in ios 11 below
Check the following example
UIImage *imgCart = [self imageWithImage:[UIImage imageNamed:#"ic_cart"] scaledToSize:CGSizeMake(35, 35)] ;
imgCart = [_utils changeColorOf:imgCart to:[UIColor whiteColor]];
UIButton *btnCart = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnCart addTarget:self action:#selector(btnCartClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnCart setBackgroundImage:imgCart forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnCart];
It does not work when you set 'cancelButton.tintColor = [UIColor whiteColor]' in ios11. you can use the method "setTitleTextAttributes".

UIAlertController is not is window hierarchy

Im trying to show up a pop up containing a UIPickerView and UITextField.
Following is my code that I use to create the pop up.
self.alert = [UIAlertController alertControllerWithTitle:nil message:#"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleAlert];
CGRect pickerFrame = CGRectMake(0, 0, 270, 200);
UIPickerView *regionsPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
regionsPicker.tag = 1;
regionsPicker.dataSource = (id <UIPickerViewDataSource>)self;
regionsPicker.delegate = (id <UIPickerViewDelegate>) self;
[regionsPicker setShowsSelectionIndicator:YES];
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];
toolView.backgroundColor = [UIColor blackColor];
CGRect buttonFrame = CGRectMake(0, 5, 100, 30);
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.frame.size.width, 0, -button.titleLabel.frame.size.width);
UIImage *btnImage = [UIImage imageNamed:#"delete.png"];
[button setImage:btnImage forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor: [UIColor blueColor] forState: UIControlStateNormal];
[toolView addSubview:button];
[button addTarget:self action:#selector(closeDialog:) forControlEvents:UIControlEventTouchUpInside];
self.textField = [[UITextView alloc] initWithFrame:CGRectMake(30, 170, 220, 200)];
_textField.editable = NO;
_textField.hidden=true;
self.textField.text = #"Enter text...";
self.textField.font = [UIFont systemFontOfSize:15];
self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.returnKeyType = UIReturnKeyDone;
self.textField.textColor = [UIColor lightGrayColor];
self.textField.delegate = (id <UITextViewDelegate>) self;
self.textField.layer.borderWidth = 1.0f;
self.textField.layer.cornerRadius = 5;
self.textField.layer.borderColor = [[UIColor grayColor] CGColor];
[toolView addSubview:self.textField];
CGRect actionFrame = CGRectMake(80, 400, 100, 30);
UIButton *actionBtn = [[UIButton alloc] initWithFrame: actionFrame];
[actionBtn setTitle:#"Submit" forState:UIControlStateNormal];
[actionBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[actionBtn setBackgroundColor:[UIColor purpleColor]];
[self.textField setUserInteractionEnabled:YES];
[toolView setUserInteractionEnabled:YES];
[self.alert.view setUserInteractionEnabled:YES];
[self.alert.view addSubview:regionsPicker];
[self.alert.view addSubview:self.textField];
[self.alert.view addSubview:toolView];
[self.alert.view addSubview:actionBtn];
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:_alert animated:YES completion:nil];
It works fine.However, if Im trying to enter text in the UITextField by tapping it, the keyboard doesn't appear and hence Im not be able to type anything within the UITextField. How can I sort this out?
the reason is you added textField, button, all those inside toolview , but the toolview height is very less
change your toolView height from 44
into 500 or else (combination of textfield, button + 50).
Change below statement
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];
to
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 500.f)];
Update
change this line
[self presentViewController:alert animated:YES completion:nil];
into
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:alert animated:YES completion:nil];
Update New
UIViewController *rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}else
{
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
}
[rootViewController presentViewController:alert animated:YES completion:nil];
Swift
var rootViewController: UIViewController = UIApplication.sharedApplication().delegate.window.rootViewController
if (rootViewController is UINavigationController.self) {
rootViewController = ((rootViewController as! UINavigationController)).viewControllers[0]
}
else {
while rootViewController.presentedViewController {
rootViewController = rootViewController.presentedViewController
}
}
rootViewController.presentViewController(alert, animated: true, completion: { _ in })
Try this it may help you:-
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:#" Your message " preferredStyle:UIAlertControllerStyleAlert];
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 200)];
toolView.backgroundColor = [UIColor blackColor];
CGRect buttonFrame = CGRectMake(0, 5, 100, 30);
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button setTitle:#"Image" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[toolView addSubview:button];
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 270, 44)];
txtField.borderStyle = UITextBorderStyleRoundedRect;
txtField.backgroundColor = [UIColor grayColor];
txtField.font = [UIFont systemFontOfSize:15];
txtField.placeholder = #"enter text";
txtField.autocorrectionType = UITextAutocorrectionTypeNo;
txtField.keyboardType = UIKeyboardTypeDefault;
txtField.returnKeyType = UIReturnKeyDone;
txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtField.delegate = (id<UITextFieldDelegate>)self;
[toolView addSubview:txtField];
[txtField setUserInteractionEnabled:YES];
[toolView setUserInteractionEnabled:YES];
[alertController.view setUserInteractionEnabled:YES];
[alertController.view addSubview:toolView];
[self presentViewController:alertController animated:YES completion:nil];

How to remove sub views from parent view

Hi i am beginner in ios and i am trying to add multiple child views programmatically on my parent class they are adding fine(here i am loading child views from background class)
But when i add second child view on my parent view class i want to remove first child view on my parent view class but it is not removing my code is below please help me some one
My code:-
child class:-
#import "MainView1.h"
#interface MainView1 ()
#end
#implementation MainView1
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)loadView1 :(UIView *)myview :(int)viewValue :(UIViewController*)VC
{
UIView * firstview;
UIView * secondview;
if (viewValue == 1) {
[firstview willRemoveSubview:myview];
[secondview willRemoveSubview:myview];
firstview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[firstview setBackgroundColor:[UIColor yellowColor]];
UIButton *addProject=[[UIButton alloc]init];
addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(100, 285, 100, 18);
addProject.backgroundColor = [UIColor redColor];
[addProject setTitle:#"Show View" forState:UIControlStateNormal];
[addProject addTarget:VC action:#selector(ProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[firstview addSubview:addProject];
[myview addSubview:firstview];
}
else {
[firstview willRemoveSubview:myview];
[secondview willRemoveSubview:myview];
secondview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 400)];
[secondview setBackgroundColor:[UIColor greenColor]];
UIButton *addProject1=[[UIButton alloc]init];
addProject1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject1.frame = CGRectMake(200, 285, 100, 18);
addProject1.backgroundColor = [UIColor redColor];
[addProject1 setTitle:#"Show View1" forState:UIControlStateNormal];
[addProject1 addTarget:VC action:#selector(ProjectPressed123:) forControlEvents:UIControlEventTouchUpInside];
[secondview addSubview:addProject1];
[myview addSubview:secondview];
}
}
parent class:-
when i clicked on below ProjectPressed button action i want to add second child view on my parent view class, It's adding fine but first child view is not removed from parent view class
#import "ViewController.h"
#import "MainView1.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MainView1 * m1 = [[MainView1 alloc]init];
[m1 loadView1:self.view :1 :self];
}
- (void)ProjectPressed:(id)sender
{
MainView1 * m1 = [[MainView1 alloc]init];
[m1 loadView1:self.view :2 :self];
}
Try this
[firstview removeFromSuperview]
[secondview removeFromSuperview]
Please try now
-(void)loadView1 :(UIView *)myview :(int)viewValue :(UIViewController*)VC
{
UIView * firstview;
UIView * secondview;
for (UIView *view in [myview subviews])
{
[view removeFromSuperview];
}
if (viewValue == 1) {
firstview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[firstview setBackgroundColor:[UIColor yellowColor]];
UIButton *addProject=[[UIButton alloc]init];
addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(100, 285, 100, 18);
addProject.backgroundColor = [UIColor redColor];
[addProject setTitle:#"Show View" forState:UIControlStateNormal];
[addProject addTarget:VC action:#selector(ProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[firstview addSubview:addProject];
[myview addSubview:firstview];
}
else {
secondview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 400)];
[secondview setBackgroundColor:[UIColor greenColor]];
UIButton *addProject1=[[UIButton alloc]init];
addProject1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject1.frame = CGRectMake(200, 285, 100, 18);
addProject1.backgroundColor = [UIColor redColor];
[addProject1 setTitle:#"Show View1" forState:UIControlStateNormal];
[addProject1 addTarget:VC action:#selector(ProjectPressed123:) forControlEvents:UIControlEventTouchUpInside];
[secondview addSubview:addProject1];
[myview addSubview:secondview];
}
}

UIBarButtons too close to the edges of UINavigationBar inside UIPopoverController

The UIBarButtonItems in the UINavigationBar inside of our UIPopoverController are hugging the left and right sides of the nav bar:
Here is the CustomPopoverController that we implemented. It used to hug the top of the navigation bar as well, but configurePopoverNavBar fixed that.
#implementation CMCommentPopoverController
- (id)initWithCaseId:(NSString *)cid andViewController:(CMNoteViewController *)cv forView:(UIView *)v {
self = [super initWithContentViewController:[[UINavigationController alloc] init]];
self.popoverNav= (UINavigationController *)self.contentViewController;
self.caseId = cid;
self.dvController = cv;
//size the popover
CGRect popoverRect = [CMMiscUtil getPopoverRect];
[self setPopoverContentSize:CGSizeMake(popoverRect.size.width, popoverRect.size.height)];
[self presentPopoverFromRect:CGRectMake(popoverRect.origin.x, popoverRect.origin.y, popoverRect.size.width, popoverRect.size.height) inView:v permittedArrowDirections:0 animated:YES];
[self setUpNav];
return self;
}
- (void) setUpNav {
[self.popoverNav pushViewController:self.dvController animated:NO];
self.popoverNav.navigationBar.topItem.title = #"Comments";
//add the buttons to the nav bar of the popover nav controller
self.popoverNav.navigationBar.topItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Add Comment" style:UIBarButtonItemStylePlain target:self action:#selector(notesAction:)];
self.popoverNav.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(closeAction:)];
[self configurePopoverNavBar:self.popoverNav];
}
-(void) configurePopoverNavBar:(UINavigationController *)popoverNav {
UINavigationBar *navigationBar = popoverNav.navigationBar;
UIView *contentView = nil;
for (UIView *view in popoverNav.view.subviews) {
if ([[NSString stringWithFormat:#"%#", [view class]] isEqualToString:#"UILayoutContainerView"])
contentView = view;
}
[navigationBar setFrame:CGRectMake(navigationBar.frame.origin.x, 0, navigationBar.frame.size.width, navigationBar.frame.size.height)];
[contentView setFrame:CGRectMake(contentView.frame.origin.x, 0, contentView.frame.size.width, contentView.frame.size.height+50 + navigationBar.frame.size.height)];
[popoverNav.view bringSubviewToFront:contentView];
for (UIView *customView in contentView.subviews)
customView.frame = CGRectMake(customView.frame.origin.x, customView.frame.origin.y + navigationBar.frame.size.height, customView.frame.size.width, customView.frame.size.height);
[contentView addSubview:navigationBar];
[contentView bringSubviewToFront:navigationBar];
}
...
Create a custom view with a UIButton and create a UIBarButtonItem with this custom view.
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 80)];
UIButton *customButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 70)];
[customButton setTitle : #"Add Comment"];
[customButton addTarget:self action:#selector(notesAction:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:customButton];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:customView];
self.popoverNav.navigationBar.topItem.rightBarButtonItem = rightBarButton;
change the customView and customButton frame according to your requirement.
Fixed this issue by putting the UINavigationBar inside a UIView that extended 10px on either side:
CGRect navBarFrame = navigationBar.frame;
navBarFrame.origin.y = 0;
UIView * navBarContainer = [[UIView alloc] initWithFrame:navBarFrame];
navBarContainer.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"header.png"]];
[navigationBar setFrame:CGRectMake(navBarFrame.origin.x + 10, navBarFrame.origin.y, navBarFrame.size.width - 20, navBarFrame.size.height)];
...
[navBarContainer addSubview:navigationBar];
[contentView addSubview:navBarContainer];
...

iPad popover presentpopoverfrombarbuttonitem

I have added a few buttons to the right side of the navigation bar with the following:
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
customView.backgroundColor = [UIColor clearColor];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:#"toc.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(tableOfContentsAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:#"bookmark.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(bookmarkButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[customView release];
[segmentBarItem release];
This works well. For both buttons I show a popOver as shown below
- (void) bookmarkButtonAction
{
BookmarksViewController* content = [[BookmarksViewController alloc] initWithOrientation:lastOrientation selectedPage:selectedPage];
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:content];
CGSize size = content.view.frame.size;
aPopover.popoverContentSize = size;
aPopover.delegate = self;
self.bookmarksPopoverVC = content;
self.bookmarksPopoverVC.popUpController = aPopover;
[content release];
[aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[aPopover release];
bookmarksShowing = YES;
}
The problem is that I am using presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem and this shows the top arrow in the middle of the two buttons. How can I attach the arrow to each button?
instead of using this line:
aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem
You may better try this line:
aPopover presentPopoverFromBarButtonItem:sender
I think that would solve your problem
try this:
- (IBAction)products:(id)sender {
UIButton* btn = (UIButton *)sender;
[productsPopover presentPopoverFromRect:[btn bounds] inView:btn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Works like a charm

Resources