center title and button in a custom sized UIAlertView iOS - ios

I am working with alertViews to load to them different objects, such as textFields and others.
With textFields there is no problem. I have successfully added a UIPickerView as a subview of my alertView and I had resized the alertView.frame to hold the pickerView properly, but then the title and the button in the alertView are not centered.
I tried with many of the options [alertView …function…] but none seems to work with this issue. This looks bad in a custom sized alertView. Any suggestions?
Thanks folks!

to solve the Title issue, I got inspiration from this post: Custom AlertView With Background
First of all, when the App presents the alertView, I called a NSLog to get:
the pickerView's (my subview) width
the _titleLabel's width
the _titleLabel's starting position (MinX and MinY)
then I resized the _titleLabel frame and that was all!
textAlignment is Center by default so all I had to do was resize the label frame. other possible method is to use
[theTitle CGAffineTransformMakeTranslation(newXValue, 0];
Y value is 0 because I didn't want it to move vertically. But the described method is cleaner to me.
so what I added to the method that creates&presents my alertView was this:
NOTE: this code HAS to go BEFORE the addition of any additional view to the alertView!
//... alertView creation and display
//create a title object to edit titleLable properties
UILabel *theTitle = [pickers valueForKey:#"_titleLabel"];
// set frame for titleLabel to begin where it currently begins in both X and Y, set its width to the width of th e picker +/- the difference in X in case they don't have the same MinX value, and set the height it already has
theTitle.frame = CGRectMake(CGRectGetMinX(theTitle.frame), CGRectGetMinY(theTitle.frame), CGRectGetWidth(pkPais.frame)+6, CGRectGetHeight(theTitle.frame));
//...add subviews and set its frames (in my case, a pickerView)
as for the button, i found in the UIAlertView.h file that buttons cannot be customized...so if someone has any tip on this issue, any help will be appreciated.
hope this helps someone else!
UPDATE: As I mentioned, Apple said buttons in alertViews cannot be customized, so there are two alternatives: create a custom button and add it as subview or have no button at all. In my case I did so, and called [myAlertView dismissWithClickedButtonIndex:-1 animated:YES] in the didSelectRow...inComponent part of the pickerView code.
This worked for me just fine, but I am open to new ideas!!
See ya!

Related

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 uilabel stay on screen when scrolling

In my app I was giving some information like "connection error" or "image saved" when users
caused some events. Instead of using alert view, I'd like to show labels with those text
on screen for
about 2-3 secs and disappear automatically.
Most like what Android apps would do like this.
(sorry for a link rather than post image due to low rep.)
As I did [superView addSubview:label] my label was attached on the back view and moved by scrolling.(superView = tableView)
How could I keep it at a fixed position of screen when user's scrolling it's superView?
prefer simple solution without using 3rd party...
tho any advance would be appreciated.
just add a label to the superview and make it hidden
label.hidden = YES; // write this in viewDidLoad()
And in event action unhide the label
label.hidden = NO;
and then set a timer so that after 2 or 3 second the label will disapear.
//inside event action
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(hideLabel) userInfo:nil repeats:NO];
and in the selector method write code for hiding the label
- (void) hideLabel
{
_lblClick.hidden = YES;
}
I hope this is what you want......
After you add the view to your superView , You can use the [superView bringSubviewToFront:<#(UIView *)#>] method. This will block some of your scrollView scrolling space, but will keep your label in the frontmost part of your view.
You can use UIView's animation to add to show and hide the label
Don't add the label to scrolling superView. Instead add it to parent of superView or the viewControllers primaryView(by default self.view) and use hidden property. Use autolayout to position the label for various layouts.

Empty white space above UITableView inside a UIView

I'm trying to position a TableView inside my ViewController view but leaving a 44 height gap between the bottom of the navigation bar and the top of the table. I then wanted to place a UITextField inside that gap to act as a stationary header. For some reason, the TableView has an empty white space above the start of the "Prototype Cells". Its just white space. Here is what it looks like in the storyboard.
When viewing the app display, this is what it looks like:
When scrolling the table, it goes all the way up to the correct place:
Try to look in the 'attribute inspector' (in the right menu) of the Participants ViewController.
Check for the option 'Extend Edges' and uncheck the 'Under Top Bars', and then relocate your tableview.
Another possible solution is to just uncheck the option 'Adjust Scroll View Insets'.
The reason is that when the viewController extends its edges, let's say under the top bar, the tableView's scrollView automatically adjusts its inset top, so that the content of the tableView will start exactly under the top bar. But in your case this is not needed, since your tableView itself starts under the bar.
Focus on the ViewController and got to the Attribute Inspector tab:
Try this one:
self.tableView.contentInset = UIEdgeInsetsMake(-65, 0, 0, 0)
Just add this in you ViewDidLoad method
self.automaticallyAdjustsScrollViewInsets = NO;
change your table view style from grouped to plain
You should not need to change the default setting for Extend Edges.
Looks like the real problem is a blank table header view in your storyboard. It's showing in the screenshot you provided of your storyboard, right below the Enter Name view and right above the Prototype Cells view. Delete it.
My issues is, I set tableHeaderView as new UIView like this
self.tableHeaderView = UIView(frame: .zero)
Remove this line, the issue is gone. Try this:
//self.tableHeaderView = UIView(frame: .zero)
This is the 2022, iOS 15 solution if anyone stumbles upon this.
if #available(iOS 15.0, *) {
UITableView.appearance().sectionHeaderTopPadding = CGFloat(0)
}
I just found a solution for this.
In my case, i was using TabBarViewController, A just uncheck the option 'Adjust Scroll View Insets'. Issues goes away.
https://i.stack.imgur.com/qgQI8.png
For Objective-C folks, add this in your ViewDidLoad method.
[self.tableView setContentInset:UIEdgeInsetsMake(-20, 0, 0, 0)];

Scroll textview when selecting text cover whole textview's screen area

In my application there is one UIScrollView having UITextView.
if the UITextView has 200 lines and user will select the first line and drag the last end point of selected text and user will drag that point to select more lines.
then, in a screen whatever the text seen, the user can select but the scroll view isn't automatic scrolling to select another text line.
may be it's quite confusing to understand but i don't know exactly what to ask for this question.
for understanding i am putting image here. in image that blue colored last end point will drag down then UITextView should scroll. that functionality i want in my application.
any types of idea, code,link will be great help...
EDIT :
The text view is fit to 200 lines.
means the text height is that much that 200 lines of text having.
so i have to scroll the scroll view and for that need any event will call on drag down the end point.
Write this And all the problem will resolve Bro ..... Do it
UITextView *lblNews=[[UITextView alloc]init];
lblNews.textColor = [UIColor blackColor];
lblNews.userInteractionEnabled=YES;
lblNews.scrollEnabled=YES;
lblNews.editable=NO;
lblNews.dataDetectorTypes = UIDataDetectorTypeAll;
lblNews.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
[self.view addSubView:lblNews];
It may be that UIScrollView is intercepting the touch when you drag your finger over it.
For a test, I would try setting the UIScrollView userInteractionEnabled = NO, and see if that allows the auto scrolling of the UITextView.
If it does then I would make a delegate for the UITextView which for delegate method textViewDidBeginEditing sets UIScrollView userInteractionEnabled = NO;
and for delegate method
textViewDidEndEditing sets UIScrollView userInteractionEnabled = YES;
similarly, for the UITextView you can try setting exclusiveTouch = YES and back to NO in the delegates, and see if that helps.

Finding The Center of a View

I have an iphone app with 2 ViewControllers . Both screens(viewcontrollers) show a loading screen. I create the loading screen programmatically:
UIView *loadingScreen = [[UIView alloc] initWithFrame:CGRectMake(100,200,144,144)];
loadingScreen.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
//{.. Other customizations to loading screen
// ..}
[self.view addSubview:loadingScreen];
For some reason, the second viewcontroller's loadingScreen is significantly lower and it isn't centered on the screen. The first viewcontroller works perfectly and is dead center like I want.
The second viewcontroller is a UITableView and it shows the uinavigationbar, whereas the first viewcontroller doesn't show the uinavigationbar. Also, I use storyboard for my app.
I've outputted to the NSLog self.view.frame.size.height and loadingScreen.center in both instances and THEY HAVE THE SAME COORDINATES! So, not sure why it is showing up lower. Any ideas why the second loadingScreen is lower and how to fix? Thanks!
You mention that one screen displays a UINavigationBar while the other does not. When you display a navigation bar, it offsets the rest of your view - in this case by shifting it down.
There are two quick fixes. You can either adjust your center point up by the size of the UINavigationBar (65 pts - unless it's a custom UINavigationBar and you've changed its size) or you can set the "Adjust Scroll View Insets" value to false in the attributes inspector.
The latter is probably the easiest and comes most recommended. Note though, that the top of your UITableView will now be underneath the UINavigationBar.
My final note would be that if you wanted to do it programmatically than in your UITableView's delegate you can call
- (BOOL)automaticallyAdjustsScrollViewInsets
{
return NO;
}

Resources