Change Position Of a UIView programatically - ios

There is an Arabic Page in my app and The "Date Field" in the Arabic Page must align to the right side of the view(right now, it is to the left). I have tried the following code
-(void)changeDatePosition{
if (!_isEnglish) {
CGRect currentDatePostion=_viewForDate.frame;
[_viewForDate removeFromSuperview];
currentDatePostion.origin.x+=currentDatePostion.size.width;
_viewForDate.frame=currentDatePostion;
[_baseView addSubview:_viewForDate];
}
}
But that did not help. The view positioned itself to the top of the navigation bar(no change in the x co-ordinate,either). I have used autolayout to align the left leading space of "ViewForDate"(the date field) to the leading space of "PlacesView"(The View on Top of the Date Field). But I set its priority to 250. HOw can I solve this problem?

Hook an IBOutlet to the leading constraint of the date view and set its value to 0. And then try this code:
-(void)changeDatePosition{
if (!_isEnglish) {
_dateViewLeadConstraint.constant = x;
}
//x - amount by which view should move right, you can calculate based on your view setup
}

-(void)changeDatePosition{
if (!_isEnglish) {
CGRect currentDatePostion=_viewForDate.frame;
[_viewForDate removeFromSuperview];
float ScreenWidth=_viewForDate.frame.size.width;
currentDatePostion.origin.x=ScreenWidth-currentDatePostion.size.width;
_viewForDate.frame=currentDatePostion;
[_baseView addSubview:_viewForDate];
}
}
can you try it please.May be it solve your problem

Try this code.
currentDatePosition = CGRectMake(self.view.frame.size.width - currentDatePosition.size.width, currentDatePosition.frame.origin.y, currentDatePosition.frame.size.width, currentDatePosition.frame.size.height);

I have found a solution to the problem and quite happy about it.
The DateView has a Left Alignment(Leading Space) to Places That is set to 0. I made a right alignment for it too(at 144). Then I created Outlets for them namely, leftAlignment(=0) and rightAlignment(=144) and wrote this method.
-(void)changeDateLocation{
if (!_isEnglish) {
_leftAlignment.priority=UILayoutPriorityDefaultLow;
_rightAlignment.constant=0;
_rightAlignment.priority=UILayoutPriorityDefaultHigh;
}
else{
_leftAlignment.priority=UILayoutPriorityDefaultHigh;
_rightAlignment.priority=UILayoutPriorityDefaultLow;
_leftAlignment.constant=0;
}
}
And this gave me my solution. I thank all you people who have contributed. This method is quite useful I think. I am trying this for the first time

Related

How do I resize my UITableView using Autolayout?

I have a UITableView in a ViewController in a Storyboard (not a UITableViewController). What I want to do is add a custom UIView above the TableView in code. When the View is not there, the TableView's top anchor is anchored to PrimaryNavCollectionViewOutlet. I store this constraint as an outlet, and then if I have to add the View, I can use this outlet to remove the storyboard constraint.
I then constrain the inserted View to be below where the TableView was, and constraint the TableView to be below that.
Here's my code:
if (_viewAboveTableView != null)
{
TableView.RemoveConstraint(TableViewTopConstraint);
View.AddSubview(_viewAboveTableView);
_viewAboveTableView.TranslatesAutoresizingMaskIntoConstraints = false;
TableView.TranslatesAutoresizingMaskIntoConstraints = false;
_viewAboveTableView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor).Active = true;
_viewAboveTableView.TopAnchor.ConstraintEqualTo(PrimaryNavCollectionViewOutlet.BottomAnchor).Active = true;
_viewAboveTableView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor).Active = true;
_viewAboveTableView.BottomAnchor.ConstraintEqualTo(TableView.TopAnchor).Active = true;
}
Unfortunately, when I run it, I can't see _viewAboveTableView. I feel like I'm missing something easy, but I can't figure out what. I've tried LayoutIfNeeded() and a few other methods on the View, but they don't make it appear. What have I missed?
Add height constraint to _viewAboveTableView.
Or
Make sure _viewAboveTableView has content that will specify intrinsic content size -- it should have properly laid-out subviews with content size (intrinsic or explicit via layouts. e.g. image, label etc.)
My guess is _viewAboveTableView is not getting inflated because it doesn't specify intrinsic content size (height > 0). If it's wrong constraints, you should be able to see it in the console.
It looks like the problem with what I was doing was that TableView.RemoveConstraint(TableViewTopConstraint); didn't have the intended effect.
From this answer I learned that RemoveConstraint is, or is about to be, deprecated in favour of TableViewTopConstraint.Active = false;. This made my code work the way that was intended.

UITextField: adjustsFontSizeToFitWidth behaviour/ issue

I have an issue with the usage of textField’s adjustsFontSizeToFitWidth. I have a textField with font size of 50 and a maximum width of 300 (This is a less than or equal to constraint). TextField is placed centre vertically and horizontally using auto layout. Now when I run the app, the text is being shrinked from the beginning itself. I know this may be because the system thinks the bounds of Texfield is not enough… but I gave the control to auto layout to figure that out and I assume it should work. Below is the snapshot… you can see the big placeholder and then suddenly the text shrinks… Any thoughts? Am I doing something wrong, I was trying to avoid manual calculation of width..
SourceCode Sample
Set leading/trailing space to text field like 15 pt from left safe area and 15 from Button so that it could automatically increase the font size. As now it shrinks the font to minimal 17 (which is set in storyboard) as the width is not enough.
Okay!! Finally I found a way, the idea was to check if the width is greater than the defined width limit... when the width is more I set adjustsFontSizeToFitWidth = true else set to false ... for some reason the solution didnt work when I clear the text... then I tried programatical approach and it started working .... dont know what was the difference... anyways I updated the source..... if there is a better solution let me know.
#IBAction func textChanged(_ txtField: UITextField) {
if txtField.frame.width >= (view.frame.width * widthMultiplier).rounded() {
txtField.adjustsFontSizeToFitWidth = true
} else {
txtField.adjustsFontSizeToFitWidth = false
}
}

Why does sizeThatFits() return a size that is too small?

I'm learning swift with cs193p and I have a problem with UITextView.sizeThatFits(...). It should return a recommended size for popover view to display an [int] array as a text. As you can see in Paul Hegarty's example (https://youtu.be/gjl2gc70YHM?t=1h43m17s), he gets perfectly-fit popover window without scrollbar. I'm using almost the same code that was in this lecture, but instead i've got this:
the text string equals [100], but the sizeThatFits() method is returning a size that is too small to display it nicely, even though there is plenty of free space.
It is getting a bit better after I've added some text, but still not precise and with the scrollbar:
Here is the part of the code where the size is being set:
override var preferredContentSize: CGSize {
get {
if textView != nil && presentingViewController != nil {
// I've added these outputs so I can see the exact numbers to try to understand how this works
print("presentingViewController!.view.bounds.size = \(presentingViewController!.view.bounds.size)")
print("sizeThatFits = \(textView.sizeThatFits(presentingViewController!.view.bounds.size))")
return textView.sizeThatFits(presentingViewController!.view.bounds.size)
} else { return super.preferredContentSize }
}
set { super.preferredContentSize = newValue }
}
What should I do so this will work in the same way as in the lecture?
It looks like there are 16 pt margins between the label and its parent view. You need to take that into account when returning the preferred size of the popover.
You should try both of the following:
Add 32 to the width that's returned from preferredContentSize
In Interface Builder, clear the layout constraints on your UILabel, then re-add top, bottom, leading, and trailing constraints and make sure that "Constrain to Margins" option is not enabled.
Finally, instead of overriding preferredContentSize, you can simply set the preferredContentSize when your view is ready to display, and you can ask Auto Layout to choose the best size:
override func viewDidLayoutSubviews() {
self.preferredContentSize = self.view.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
}
If your layout is configured correctly, systemLayoutSizeFitting(UILayoutFittingCompressedSize) will return the smallest possible size for your view, taking into account all of the margins and sub-views.

Centering node vertically in horizontal ASStackLayout

I'm wonder what is the correct way to center textNode inside vertical ASSStackNode which is inside horizontal ASStackNode without setting any sizes?
I've tried different options with ASCenterLayout / ASRelativeLayout around vertical layout, flewGrow, flexShrink.
Only possible way that I see is static layout with minimum height around vertical layout. But wanna find if there are another way.
Images to better picture:
My layout structure:
What I get when one of text fields is empty:
What I want to see:
If your ASTextNode is empty or nil. Isn't mean node not exist. If you want hide node if it empty, you must write a simple logic for it. For exlude it Spec from calculation. What i use on ADK1.9:
- (ASLayoutSpec *)layoutSpecThatFits: (ASSizeRange)constrainedSize
{
NSMutableArray *arrayOfChildren = [NSMutableArray new];
[arrayOfChildren insertObject:self.titleNode];
if (self.textNode.text.length > 0) {
[arrayOfChildren insertObject:self.textNode];
}
ASStackLayoutSpec *verticalElementCoreStack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:8
justifyContent:ASStackLayoutJustifyContentCenter alignItems:ASStackLayoutAlignItemsStretch
children:arrayOfChildren];
return verticalElementCoreStack;
}

iOS 5: UILabel change size dynamically

I am using storyboard and custom cells. In one of my labels I drugged on custom cell I need to print date. If date is arabic one, then I need to make label wider and print both dates -> arabic and christian.
My problem is that I am trying to access my label and change size:
if(str.length==ARABIC_DATE)
{
cell.lblYears.layer.frame = CGRectMake(0.0, 0.0, 50.0, 10.0);
cell.lblYears.backgroundColor=[UIColor blackColor];
cell.lblYears.text=str;
}
else
{
cell.lblYears.text=str;
}
The result is an additional label in wrong place (0.0, 0.0..). How to make that code will not produce an additional label, but will change the existing.
Thanx in advance.
You are not creating a second label in this code. You are placing an existing label in the (0,0) coordinate and setting 50 to the width and 10 to the height.
I don't know what your problem is exactly, but there is nothing wrong in that piece of code.

Resources