iOS UI Not showing where it should be - ios

Good day, I am sitting with an issue regarding my UI...
I am busy developing an app for iPhones, but the UI does not work together well...
I set a custom tableviewcell, and have a button that should be at the end of the cell, but the display is not correct, however if i take the frame it seems to be correct. Here is a screenshot of what happens when I scroll down...
In my code I set the arrow button with the following line of code:
cell.button.frame = CGRectMake(self.tableview.frame.size.width-60, (height-40)/2, 40, 40);
The idea is that I have a 20px space behind the button...
Note I am not using AutoLayout etc...
And if i run the code for the button and I log all the positions of the button, the positions are all 100% the same...
Any help will be appreciated...

You should add your code for adjusting your button frame in the layoutSubViews method of your cell class
override func layoutSubviews() {
super.layoutSubviews()
button.frame = CGRectMake(self.bounds.size.width-60, (self.bounds.size.height-40)/2, 40, 40);
}

Related

How to prevent iOS from resizing your UIViewController's view after return from background

I have a UIViewController that displays a form with several text fields. In order to prevent the text fields from getting blocked by the keyboard, I resize the controller's view when the keyboard appears and disappears.
However, when the keyboard is up, the user presses the home button, and then returns to the app, the controller's view will be resized again to the size it was before the keyboard was up and the keyboard will still be showing.
What's causing my controller's view to be resized on return from background, and how can I prevent it?
Maybe you need to nest a UIView,for example
_backgroundView = [UIView new];
_backgroundView.backgroundColor = [UIColor whiteColor];
_backgroundView.frame = CGRectZero;
[self.view addSubview:_backgroundView];
[_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(self.view);
make.height.mas_equalTo(self.view.mas_height);
}];
then you need add your custom UIView to this backgroundView.
as you said,UIViewController's view will be resized after return from background. so you can nest a UIView of the same size as self.view,and add your custom UIView to this UIView.
In order to prevent the text fields from getting blocked by the keyboard, you can resize this backgroundView when the keyboard appears and disappears. and this time when you click the home button to enter the background or return from background,self.view won't be resized and backgroundView won't be resized too.
Although it is a bit of a hassle, this will solve your problem and will not affect the user experience anymore. And if you have a better solution, please let me know
It sounds like you are setting the frame and not using autolayout. When the view reappears viewDidLayoutSubviews gets called and your frame gets recalculated obliterating your previous change. You can either:
1) Move your frame to viewDidLayoutSubviews and change its size only if the keyboard is showing.
2) Use autolayout and simply pull up your bottom constraint .constant by an amount equal to your keyboard height.
In both cases you should call layoutIfNeeded to trigger autolayout/viewDidLayoutSubviews when the keyboard appears/disappears. This behavior is a good example of why you should not manipulate your frames outside of viewDidLayoutSubviews except for transitory animations.

Displaying button in subview in a stackview

I am trying to display the facebook login button in UIView but am running into some issues. I have a UIView that is currently within a stackview and I think that's causing some issues.
This screenshot shows the UIView I am trying to add the button into:
And this is where the FB button is displaying:
So I have two issues. I want the FB login button to fit the size of the UIView completely, and also be in the same location.
I have the following piece of code:
override func viewDidLoad() {
super.viewDidLoad()
let fbLoginButton = FBSDKLoginButton()
let convertToSuperView = loginStackView.convert(fbLoginButton.frame, to: self.view)
fbLoginButton.frame = CGRect(x: fbLoginView.bounds.origin.x, y: fbLoginView.bounds.origin.y, width: convertToSuperView.width, height: convertToSuperView.height)
fbLoginButton.clipsToBounds = true
fbLoginView.addSubview(fbLoginButton)
}
From reading documentation I got that the bounds is for getting the points at which something is located on the screen. I tried converting the bounds of the UIView from within the stackView to the parent view, and then giving the button the same frame as the UIView.
This clearly is not working so some assistance would be helpful. If you could explain why issues like this happen that would help me tackle issues like this in the future.
EDIT: Thanks to comments from #pierce I got it to look a little better by adding the fbLoginButton as a subview to fbLoginView.
Here I still have one issue, the frame isn't equal in size to the UIView
I also updated the code snippet above to reflect this. I have tried playing around with the sizing, but run into issues.
When I set the rect width/height to use the fbLoginView width and height, the login button is WAY too large, even with clipsToBounds its too large. When I try to center the button, it disappears.

Change UISearchBar magnify icon color and position

I have a UISearchBar and I would like to change the position of the initial magnify icon (the one that appears in the middle of the UISearchBar) as well as the color or icon.
So far I changed the tint and the icon image.
However, the new icon shows up only if I test the app on a simulator but on an actual device(both run iOS 9.3) it still shows the default icon.
UISearchBar.appearance().setImage(UIImage(named: "SearchbarIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
As for the magnify icon position, I want it in the left side, where it shows up if I activate the UISearchBar.
I found quite a lot of answers around here many of the provided solutions don't work on iOS 8.0+ or are in Objective-C and I am having some problems understanding them.
I tried to add a custom background containing the icon but it shows up at the bottom of the search bar while the icon is still there:
The background shows up OK if I change the background offset for Y to -44 (the height of the UISearchBar) but I can no longer type since it seems the entire text field is pushed up. I also tried to change the vertical offset for the SearchText to 44 to compensate but no luck. To be honest, I am not sure what Search Text and the Background offsets are supposed to do but I decided to give them a try.
Is there any way to accomplish this? Or maybe a different approach?
You can adjust the position of the search bar icon using
func positionAdjustmentForSearchBarIcon(_ icon: UISearchBarIcon) -> UIOffset
See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/occ/instm/UISearchBar/positionAdjustmentForSearchBarIcon:
You can use:
uiSearchBar.setPositionAdjustment(UIOffset, for: UISearchBar.Icon)
replace UIOffset with your desired offset value UIOffset(horizontal: CGFloatvertical: CGFloat) and UISearchBar.Icon with .search
#Michael - Thanks for all the help.
I managed to grab the UISearchBar's UITextField:
func customizeSearchBar()
{
for subview in srcRegimenSearchBar.subviews
{
for view in subview.subviews
{
if let searchField = view as? UITextField
{
let imageView = UIImageView()
let image = UIImage(named: "SearchBarIcon.png")
imageView.image = image;
imageView.frame = CGRectMake(0, 0, 0, 0)
/*imageView.frame = CGRectMake(100, 0, 20, 19)*/
searchField.leftView = imageView
searchField.leftViewMode = UITextFieldViewMode.Always
}
}
}
}
I wanted to change the position since I have access to the frame but it seems only the the top and height can be modified, at least the way I tried so I set the height and width to 0 (I couldn't find a way to make it nil or remove it completely) and I added an UIImageView with the new icon over in the left side of the UISearchbar and added a custom horizontal offset for the tint.
Not the best solution out there, I'm sure of it, but for now it works.

Moving UIView from One Tab to another

I'm making this type of screen.
I've used 3 UIButton and programmatically gave them border. The little red line is a UIView. I've provided a constraint to UIView Equal Widths as Trending Button.
Now my question that when user taps the next button this UIView should move to the next one. View Controller is 600x600 and next button is at 200x0 so if I change the value of UIView to 200 that will be hard coded and it'll alter according to the screen size. I want it to be perfect and don't know any other way.UPDATE:I'm using AutoLayout so for this purpose I used [self.buttonBottomView setTranslatesAutoresizingMaskIntoConstraints:YES]; and when I run the app the buttons were messed up like in the screenshot. I've applied some constraints on buttons.
You can use NSLayoutConstraint ,change it's LeadingConstaint when you tap on the next button.
Code:
- (void)changeSelectedByTapView:(UIView *)tapView {
_currentSelectedView = tapView;
self.lineImageViewLeadingConstaint.constant = tapView.frameX;
self.lineImageViewWidthConstaint.constant = tapView.width;
[UIView animateWithDuration:0.5f animations:^{
[self.lineImageView.superview layoutIfNeeded];
}];
}
change frame of your UIView.
view.frame = CGRectMake(0+buttonWidth*i, y, width, height);
i is index which is 0 for Trending, 1 for Made by the fans and 2 for your rules.
Also when you change frame for your view, it is shown only on one button at a time.

iOS Storyboard segmented control jumps when clicked on

I have a storyboard with an imageview, a segmented control and a textview. After appearing I make some adjustments depending on the device screen size:
- (void) viewDidAppear:(BOOL)animated
{
NSLog(#"didAp");
CGSize mainFrameSize=_mainView.frame.size;
[_imageSpace setFrame:CGRectMake(0, 0, mainFrameSize.width, mainFrameSize.height*4/5)];
[_metadadataControl setFrame:CGRectMake(0, _imageSpace.frame.size.height, mainFrameSize.width, 30)];
[_metadataTextView setFrame:CGRectMake(0, _imageSpace.frame.size.height+_metadadataControl.frame.size.height, mainFrameSize.width, mainFrameSize.height-(_imageSpace.frame.size.height+_metadadataControl.frame.size.height))];
}
When the app launched everything looks like it should, but if I click the segmented control it jumps some points down. The above method is just called once before I clicked the control and I don't have any other setFrame method calls. It seems like it just loads the storyboard again.
Thank you for your help
rdelmar was right. It was an issue with constraints contradicting my frame settings. Switched to doing everything with auto layout and constraints afterwards.

Resources