导航栏rightbaritem图像按钮错误iOS 11
这段代码在ios10中工作正常。我得到了我的标签和一个图像按钮,这是用户的照片配置文件,圆形。。好啊但当我运行xcode 9 ios11模拟器时,我发现它太累了。当检查sim卡并获取视图并告诉xcode将我获取的视图描述为170x32或类似的东西时,按钮框必须是32x32 这是我的密码导航栏rightbaritem图像按钮错误iOS 11,ios,swift,ios11,xcode9-beta,swift4,Ios,Swift,Ios11,Xcode9 Beta,Swift4,这段代码在ios10中工作正常。我得到了我的标签和一个图像按钮,这是用户的照片配置文件,圆形。。好啊但当我运行xcode 9 ios11模拟器时,我发现它太累了。当检查sim卡并获取视图并告诉xcode将我获取的视图描述为170x32或类似的东西时,按钮框必须是32x32 这是我的密码 let labelbutton = UIButton( type: .system) labelbutton.addTarget(self, action:#selector(self.toLogin(_
let labelbutton = UIButton( type: .system)
labelbutton.addTarget(self, action:#selector(self.toLogin(_:)), for: .touchUpInside)
labelbutton.setTitleColor(UIColor.white, for: .normal)
labelbutton.contentHorizontalAlignment = .right
labelbutton.titleLabel?.font = UIFont.systemFont(ofSize: 18.00)
let button = UIButton(type: .custom)
button.addTarget(self, action:#selector(self.toLogin(_:)), for: .touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 32, height: 32)
button.setTitleColor(UIColor.white, for: .normal)
button.setTitleColor(UIColor.white, for: .highlighted)
var buttomItem : UIBarButtonItem = UIBarButtonItem()
buttomItem.customView = button
buttomItem.target = self
buttomItem.action = "ToLogin"
var labelItem : UIBarButtonItem = UIBarButtonItem()
labelItem.customView = labelbutton
labelItem.target = self
labelItem.action = "ToLogin"
if let user = PFUser.current() {
print("LOGIN : checkiando si existe usuario ")
labelbutton.setTitle(USERNAME, for: UIControlState.normal)
labelbutton.sizeToFit()
if(user["profile_photo_url"] != nil) {
print(" ENCONTRO PROFILE PHOTO URL NOT NIL Y ES \(user["profile_photo_url"])")
let photoURL = user["profile_photo_url"] as! String
let a = LoginService.sharedInstance
a.downloadImage(url: photoURL, complete: { (complete) in
if (complete) {
button.setImage(LoginService.sharedInstance.profile_photo! , for: UIControlState.normal)
button.layer.cornerRadius = 0.5 * button.bounds.size.width
// button.imageView!.contentMode = .scaleAspectFit
// button.imageView!.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
//button.imageView!.contentMode = .scaleAspectFit
//button.imageView!.clipsToBounds = true
//button.imageView!.layer.cornerRadius = 60
button.clipsToBounds = true
self.NavigationItem.rightBarButtonItems = [buttomItem,labelItem]
}
})
} else {
self.NavigationItem.rightBarButtonItem = labelItem
}
print(" EL FRAME DEL BUTTON ES \(button.frame)")
} else {
labelbutton.setTitle("Login", for: UIControlState.normal)
labelbutton.sizeToFit()
self.NavigationItem.rightBarButtonItem = labelItem
}
原因 出现此问题是因为ios 11
UIBarButtonItem
使用自动布局而不是处理帧
解决方案
如果使用Xcode 9,则应该为该图像按钮添加宽度约束
button.widthAnchor.constraint(equalToConstant: 32.0).isActive = true
button.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
PS
按钮
不是uibarbuttoneim
,而是uibarbuttoneim
内部的uibarbuttoneim
。您不应该为uibarbuttoneim
设置约束,而应该为其中的元素设置约束。好吧,新的barbuttoneim
使用自动布局,而不是处理帧
您添加到按钮的图像大于按钮本身的大小。这就是为什么按钮本身会拉伸到图像的大小。在将图像添加到按钮之前,您必须调整图像大小以匹配所需按钮的大小。感谢大家的贡献!你们说得对!。对于xcode9 ios11,您需要设置一个约束
let widthConstraint = button.widthAnchor.constraint(equalToConstant: 32)
let heightConstraint = button.heightAnchor.constraint(equalToConstant: 32)
heightConstraint.isActive = true
widthConstraint.isActive = true
我编写了一个用于设置导航栏项约束的小扩展:
import UIKit
extension UIView {
func applyNavBarConstraints(size: (width: CGFloat, height: CGFloat)) {
let widthConstraint = self.widthAnchor.constraint(equalToConstant: size.width)
let heightConstraint = self.heightAnchor.constraint(equalToConstant: size.height)
heightConstraint.isActive = true
widthConstraint.isActive = true
}
}
// Usage
button.applyNavBarConstraints(size: (width: 33, height: 33))
我还成功地实现了
intrinsicContentSize
,为我打算用作customView的任何自定义UIView子类返回一个合适的大小。即使iOS 11对导航栏使用了自动布局,也可以使其在设置帧时正常工作。以下是我为ios11和ios10或更早版本工作的代码:
func barItemWithView(view: UIView, rect: CGRect) -> UIBarButtonItem {
let container = UIView(frame: rect)
container.addSubview(view)
view.frame = rect
return UIBarButtonItem(customView: container)
}
下面是条目的组成方式:
let btn = UIButton()
btn.setImage(image.withRenderingMode(.alwaysTemplate), for: .normal)
btn.tintColor = tint
btn.imageView?.contentMode = .scaleAspectFit
let barItem = barItemWithView(view: btn, rect: CGRect(x: 0, y: 0, width: 22, height: 22))
return barItem
目标C代码现在已经过时了。但对于必须在iOS 11中构建/维护Objective C项目的用户,Swift( Karoly Nyisztor回答)对目标C有帮助
// UIView+Navbar.h
#import <UIKit/UIKit.h>
@interface UIView (Navbar)
- (void)applyNavBarConstraints:(CGFloat)width height:(CGFloat)height;
@end
//----------
// UIView+Navbar.m
#import "UIView+Navbar.h"
@implementation UIView (Navbar)
- (void)applyNavBarConstraints:(CGFloat)width height:(CGFloat)height
{
if (width == 0 || height == 0) {
return;
}
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:height];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:width];
[heightConstraint setActive:TRUE];
[widthConstraint setActive:TRUE];
}
//----------
// Usage :-
[button applyNavBarConstraints:33 height:33];
//UIView+Navbar.h
#进口
@界面视图(导航栏)
-(void)applyNavBarConstraints:(CGFloat)宽度高度:(CGFloat)高度;
@结束
//----------
//UIView+Navbar.m
#导入“UIView+Navbar.h”
@实现UIView(导航栏)
-(void)ApplyNavBar约束:(CGFloat)宽度高度:(CGFloat)高度
{
如果(宽度=0 | |高度=0){
返回;
}
NSLayoutConstraint*heightConstraint=[NSLayoutConstraint constraintWithItem:self属性:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil属性:NSLayoutAttributeNotAttribute属性乘数:1常量:高度];
NSLayoutConstraint*widthConstraint=[NSLayoutConstraint ConstraintWidthItem:self属性:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil属性:NSLayoutAttributeNotAttribute乘数:1常量:宽度];
[heightConstraint setActive:TRUE];
[widthConstraint setActive:TRUE];
}
//----------
//用法:-
[按钮ApplyNavBar约束:33高度:33];
我在objective中使用了以下几行代码:
NSLayoutConstraint * widthConstraint = [customButton.widthAnchor constraintEqualToConstant:40];
NSLayoutConstraint * HeightConstraint =[customButton.heightAnchor constraintEqualToConstant:40];
[widthConstraint setActive:YES];
[HeightConstraint setActive:YES];
UIBarButtonItem* customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.leftBarButtonItem = customBarButtonItem;
谢谢快乐编码 我做了什么
在我的应用程序中,我在导航栏的rightBarButton项上添加了个人资料图像。在iOS 11之前,它工作良好,显示正常,但当更新到iOS 11时,它会改变行为,如blow
因此,我在右按钮项中添加了UIView
,并将UIButton
设置为UIView
的子视图?如下图所示
我设置了ui按钮的高度和宽度限制
我的问题解决了。不要忘记将UIView
的背景色设置为clear颜色
注意:如果您的按钮不起作用,请检查您的ui视图的
高度可能是0这里您应该将高度0更改为44或任何您想要的。同时也要执行cliptobund=true
,现在您可以设置按钮的位置,它将很好地工作
对于运行iOS 11.X的用户,以编程方式放置约束对我来说很有效但是,对于运行iOS 10.X的用户,条形按钮仍然被拉长。我猜AppStore的审查人员运行的是iOS 11.X,因此无法识别我的问题,所以我的应用程序准备好出售并上传
我的解决方案是在另一个软件中将我的图像尺寸更改为30x30(以前的图像尺寸为120x120)。更改widthAnchor
/heightAnchor
仅适用于iOS 11+设备。对于iOS 10设备,您需要采用手动更改帧的经典方式。问题是这两种方法都不适用于两个版本,因此您绝对需要根据运行时版本以编程方式进行替换,如下所示:
if #available(iOS 11.0, *)
{
button.widthAnchor.constraint(equalToConstant: 32.0).isActive = true
button.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
}else
{
var frame = button.frame
frame.size.width = 32.0
frame.size.height = 32.0
button.frame = frame
}
我创建了一个栏按钮项,然后将其添加到导航栏
private var addItem: UIBarButtonItem = {
let addImage = UIImage(named: "add")
let addButton = UIButton(type: UIButton.ButtonType.custom)
addButton.setBackgroundImage(addImage, for: UIControl.State())
addButton.frame = CGRect(x: 0, y: 0, width: (addImage?.size.width)!, height: (addImage?.size.height)!)
let addItem = UIBarButtonItem(customView: addButton)
return addItem
}()
private var contactsItem: UIBarButtonItem = {
let contactsImage = UIImage(named: "contacts")
let contactsButton = UIButton(type: UIButton.ButtonType.custom)
contactsButton.setBackgroundImage(contactsImage, for: UIControl.State())
contactsButton.frame = CGRect(x: 0, y: 0, width: (contactsImage?.size.width)!, height: (contactsImage?.size.height)!)
let contactsItem = UIBarButtonItem(customView: contactsButton)
return contactsItem
}()
在viewDidLoad()中
这里是28x28的图像。您使用导航栏中的堆栈视图了吗?@V.Khambir Nop…:/此错误报告是否存在?iOS 11使用自动布局来布局导航项。如果您需要在UIBarButtonItem
内移动uibuttonItem
,请使用button.imageEdgeInsets=UIEdgeInsets(顶部:0,左侧:20,底部:0,右侧:-20)
这对我也很有效,但有谁能解释为什么iOS 11以前从未有过这样的必要?UINavigationBar现在使用自动布局来布局它的子视图不要犯我犯过的错误:我关闭了translatesAutoResistingGMaskintoConstraints
,后来发现它完全破坏了iOS 9(但在iOS 10和11上看起来很好)类型为'UIBarButtonItem'的值在Xcode 9中没有成员'widthAnchor'
,如果可用(iOS 11.0,*)
有条件?@lorenzo gonzalez你是怎么做到的?我被困在这上面了,酷。实际上,我也需要Objective-C版本。顺便说一句,Xcode 9一直在随机调整大小
let spacerBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
spacerBarButtonItem.width = 11
navigationItem.rightBarButtonItems = [addItem, spacerBarButtonItem, contactsItem]