开发者

center title and button in a custom sized UIAlertView 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 wit开发者_StackOverflowh 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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜