UIScrollView hiding UINavigationBar - ios

I have an a UIControlView with the following hierarchy.
...
UIView
UIScrollview
textfield
button
textfield
button
NavigationBar
...
When this view loads the scrollview pushes the navigationBar off the screen or hides it? What am I doing wrong?
When I drag a srollview onto the UIViewController storyboard, should the scrollview be at 480 or around 430 so its below the navigationbar?
// === HERE IS THE CODE (there is no other custom code besides this) ======
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
DLog(#"%f", self.scrollView.contentOffset.y);
//self.scrollView.contentOffset = CGPointMake(self.view.contentOffset.x,0);
self.codeTextField.delegate = self;
self.mobileTextField.delegate = self;
[self.navBarLabel setTitleColor:[UIColor statDarkGray] forState:UIControlStateNormal];
[self.navBarLabel setTitleColor:[UIColor statDarkGray] forState:UIControlStateSelected];
[self.navBarLabel setTitleColor:[UIColor statLightGray] forState:UIControlStateHighlighted];
UIFont *font = [UIFont fontWithName:#"GothamRounded-Light" size:17];
[self.navBarLabel.titleLabel setFont:font];
//SETUP TEXTFIELDS
// --- Setup textfield designs ---------
self.codeTextField.borderStyle = UITextBorderStyleNone;
UIImage *fieldBGImage = [[UIImage imageNamed:#"code_textfield.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
[self.codeTextField setBackground:fieldBGImage];
// for left padding
self.codeTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 88, 20)];
self.codeTextField.leftViewMode = UITextFieldViewModeAlways;
self.mobileTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 88, 20)];
self.mobileTextField.leftViewMode = UITextFieldViewModeAlways;
// setup submit button
self.submitButton.titleLabel.text = #"Submit";
self.submitButton.titleLabel.font = [UIFont fontWithName:#"GothamRounded-Light" size:18];
self.submitButton.enabled = NO;
[self.submitButton setBackgroundImage:[UIImage imageNamed:#"bluebutton_disabled.png"] forState:UIControlStateDisabled];
[self.submitButton setBackgroundImage:[UIImage imageNamed:#"bluebutton_normal.png"] forState:UIControlStateNormal];
[self.submitButton setBackgroundImage:[UIImage imageNamed:#"bluebutton_selected.png"] forState:UIControlStateSelected];
// setup submit button
self.resendCodeButton.enabled = NO;
self.resendCodeButton.titleLabel.font = [UIFont fontWithName:#"GothamRounded-Light" size:18];
[self.resendCodeButton setBackgroundImage:[UIImage imageNamed:#"redbutton_disabled.png"] forState:UIControlStateDisabled];
[self.resendCodeButton setBackgroundImage:[UIImage imageNamed:#"redbutton_normal.png"] forState:UIControlStateNormal];
[self.resendCodeButton setBackgroundImage:[UIImage imageNamed:#"redbutton_selected.png"] forState:UIControlStateSelected];
// SETUP BUTTONS
// The next arrow button
UIImage *background = [UIImage imageNamed:#"Cancel_normal.png"];
UIImage *backgroundSelected = [UIImage imageNamed:#"Cancel_selected.png"];
self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.cancelButton addTarget:self action:#selector(cancelPressed:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[self.cancelButton setBackgroundImage:background forState:UIControlStateNormal];
[self.cancelButton setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
self.cancelButton.frame = CGRectMake(0 ,0,background.size.width, background.size.height);
self.cancelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.cancelButton];
self.navigationItem.leftBarButtonItem = self.cancelButtonItem;
}

The view was shown modally not pushed. And even though I designed a navbar and set one in the settings (translucent bar). It wasn't showing one?
So on the segue I was using show modally and the, no matter if it had a UIView or UIScrollView, never would show the navbar. By switching to a PUSH segue, it now shows the navbar. Fixed.
I just decided to push the view instead of modal to be done with it.

Related

UINavigationItems Stretching in iOS11

I'm updating an app to ensure that it works as expected in iOS11. The only issue I've run into is that the navigation bar looks quite different in iOS11. The position of the elements and size is not the same between versions. I've looked at my code and i'm explicitly setting frame sizes and edge insets, why are these values are not respected in iOS11?
//-- Create Right Navigation Item Buttons
// create kabob btn
UIButton *kabobBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[kabobBtn addTarget:self action:#selector(viewUsersClicked:)forControlEvents:UIControlEventTouchUpInside];
kabobBtn.clipsToBounds = YES;
[kabobBtn setImage:[UIImage imageNamed:#"KabobMenu2_0071bc_Right.png"]
forState:UIControlStateNormal];
[kabobBtn.imageView setContentMode:UIViewContentModeScaleAspectFit];
[kabobBtn setFrame:CGRectMake(0, 0, 32, 32)];
// create user profile button
UIButton *userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[userBtn setImage:[UIImage imageNamed:#"people.png"]
forState:UIControlStateNormal];
[userBtn addTarget:self action:#selector(viewUserProfileClicked:)forControlEvents:UIControlEventTouchUpInside];
[userBtn setFrame:CGRectMake(0, 0, 38, 38)];
userBtn.clipsToBounds = YES;
[userBtn.layer setCornerRadius:(38/2)];
[userBtn.layer setMasksToBounds:YES];
[userBtn.layer setBorderColor:[[UIColor blackColor]CGColor]];
[userBtn.layer setBorderWidth:1];
self.userPictureURL = [defaults objectForKey:#"userPicture"];
[userBtn setImage:[UIImage imageNamed:#"defaultUserImage.png"] forState:UIControlStateNormal];
[userBtn setContentMode:UIViewContentModeScaleAspectFill];
[userBtn setBackgroundColor:[UIColor clearColor]];
// add buttons to bar button items
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:kabobBtn];
UIBarButtonItem *barButton2 = [[UIBarButtonItem alloc] initWithCustomView:userBtn];
// add bar button items to right bar button item array
self.navigationItem.rightBarButtonItems = #[barButton,barButton2];
//-- Create Left Hamburger Button
UIButton *customOpen = [UIButton buttonWithType:UIButtonTypeCustom];
[customOpen setFrame:CGRectMake(0, 0, 30, 44)];
[customOpen setImage:[UIImage imageNamed:#"hamburger.png"] forState:UIControlStateNormal];
[customOpen setImageEdgeInsets:UIEdgeInsetsMake(10, -5, 16, 10)];
UIBarButtonItem *openItem = [[UIBarButtonItem alloc] initWithCustomView:customOpen];
if(self.showHamburger){
self.navigationItem.leftBarButtonItem = openItem;
}else{
self.navigationItem.leftBarButtonItem = nil;
}
Apparently iOS 11 uses autolayout for nav bar buttons. Use auto layout constraints to set button width and height.
[userBtn.widthAnchor constraintEqualToConstant:38.f].active = YES;
[userBtn.heightAnchor constraintEqualToConstant:38.f].active = YES;
This should fix the stretching issue.
Thanks to this answer

in landscape no showing image and button

I have added one imageview, 2 button in one view controller in code. In portrait mode its working well. But in landscape mode not able to see the image and button.
#implementation ViewController {
UIImageView* imgView; // your UIImageView
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
UIImage* img1 = [UIImage imageNamed:#"11.jpg"];
UIImage* img2 = [UIImage imageNamed:#"s2.jpg"];
imgView = [[UIImageView alloc] initWithFrame:self.view.frame]; // create a UIImageView with the same size as the view.
imgView.animationImages = #[img1, img2]; // use a nice NSArray literal to pass in your images.
imgView.animationDuration = 2.0*2.0; // 2 seconds for each image (x3 images)
[self.view addSubview:imgView]; // add UIImageView to view
[imgView startAnimating]; // start animating
// button one
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button setTitle:#"Sign Up" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[button addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Button Two
UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button1 setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button1 setTitle:#"Sign In" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[self.view addSubview:button1];
// Border color for one button
UIView *leftBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 1, 1, button.frame.size.height)];
leftBorder.backgroundColor = [UIColor colorWithRed:237/255.0f green:211/255.0f blue:246/255.0f alpha:0.6f];
[button1 addSubview:leftBorder];
}
Why its not working well for landscape. Please help me, for portrait its working well. For landscape its not working ..
It's not working in landscape because when we rotate to landscape the button and image view, as you have positioned them in your code, are located below the bottom of the screen.
You will have to implement Auto Layout. It will fix the problem. Please avoid hard coding the positions as will create problems in UI. If you want to hard code the points then make your app in either portrait mode or landscape mode.
In auto layout constraints can be set programmatically as well. Just check which is feasible for you and implement it.

Title of a Custom UIBarButtonItem from a UIButton not showing

I made a custom UIBarButtonItem from a UIButton. The custom bar button is visible, the action works, the size is correct, and the background color can be seen; however the manually set title cannot be seen. maybe background is laid over the text? I'm not sure, help?
In viewDidLoad:
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
resetButton.titleLabel.text = #"RESET";
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
You should set the title for button of using setTitle:forState: for particular control states, replace your code with below
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
[resetButton setTitle:#"RESET" forState:UIControlStateNormal]; //change this line in your code
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
Use setTitle:forState to set button title. For example
[resetButton setTitle:#"text" forState:UIControlStateNormal]

Adding two UIBarButtomItems with image programmatically

I am trying to use following code in my viewDidLoad method to add two UIBarButtonItems to the left side of the navigation bar, but only the second button is shown:
//nav bar buttons
UIView* leftButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
leftButton.backgroundColor = [UIColor clearColor];
leftButton.frame = leftButtonView.frame;
[leftButton setImage:[UIImage imageNamed:#"toTop"] forState:UIControlStateNormal];
[leftButton setTitle:#"" forState:UIControlStateNormal];
leftButton.tintColor = [UIColor redColor]; //Your desired color.
leftButton.autoresizesSubviews = YES;
leftButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
[leftButton addTarget:self action:#selector(toTop:) forControlEvents:UIControlEventTouchUpInside];
UIView* leftButtonView2 = [[UIView alloc]initWithFrame:CGRectMake(85, 0, 40, 40)];
UIButton* leftButton2 = [UIButton buttonWithType:UIButtonTypeSystem];
leftButton2.backgroundColor = [UIColor clearColor];
leftButton2.frame = leftButtonView.frame;
[leftButton2 setImage:[UIImage imageNamed:#"toSectionTop"] forState:UIControlStateNormal];
[leftButton2 setTitle:#"" forState:UIControlStateNormal];
leftButton2.tintColor = [UIColor redColor]; //Your desired color.
leftButton2.autoresizesSubviews = YES;
leftButton2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
[leftButton2 addTarget:self action:#selector(toTop:) forControlEvents:UIControlEventTouchUpInside];
[leftButtonView addSubview:leftButton];
[leftButtonView2 addSubview:leftButton2];
UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:leftButtonView];
UIBarButtonItem* leftBarButton2 = [[UIBarButtonItem alloc]initWithCustomView:leftButtonView2];
self.navigationItem.leftBarButtonItem = leftBarButton;
self.navigationItem.leftBarButtonItem = leftBarButton2;
Any help is welcome. Thank you
You're setting the same property twice so the second setting replaces the first. Try:
self.navigationItem.leftBarButtonItems = #[ leftBarButton, leftBarButton2 ];
Note this is setting leftBarButtonItem*s*, which takes an array of bar buttons.
Also, you shouldn't need the container views, they add no value. Buttons are views so you can use them directly. Setting the frame origin of the view may also cause you issues.

iPad develop:customized button on navigationBar can't see after push->pop

iPad app:I've add some customized button to navigationBar as follows:
- (void)addEditButton{
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, 50, 30);
[editButton setBackgroundImage:[[UIImage imageNamed:#"images_iPad.bundle/NavigationBar_Btn_iPad.png"] stretchableImageWithLeftCapWidth:15/2.0 topCapHeight:32/2.0] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
[editButton setTitle:#"Edit" forState:UIControlStateNormal];
editButton.titleLabel.font = [UIFont fontWithName:kAPPBOLDFONT size:12];
editButton.titleLabel.shadowColor = [UIColor blackColor];
editButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
UIBarButtonItem *editItem = [[[UIBarButtonItem alloc] initWithCustomView:editButton] autorelease];
self.navigationItem.rightBarButtonItem = editItem;
}
My problem is that:I clicked this button,it will push to another viewController, after popModelViewController:animate,I can't see this customized button, although this, it does work.and not just here ,others are the same,please help me.

Resources